Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在NSMutableArray中搜索NSString时,如何确定该对象的索引?_Objective C_Xcode_Nsmutablearray_Xcode4.3_Uisearchbar - Fatal编程技术网

Objective c 在NSMutableArray中搜索NSString时,如何确定该对象的索引?

Objective c 在NSMutableArray中搜索NSString时,如何确定该对象的索引?,objective-c,xcode,nsmutablearray,xcode4.3,uisearchbar,Objective C,Xcode,Nsmutablearray,Xcode4.3,Uisearchbar,目前,我正在使用表中显示的数据库中的数据对UISearchBar的搜索功能进行编码。我计算出搜索结果并将其保存到NSMutableArray*searchResults。这看起来是这样的: - (void) searchGrapesTableView { [searchResult removeAllObjects]; numSearchWines=0; for (NSString *str in listOfWines) { NSRange ti

目前,我正在使用表中显示的数据库中的数据对UISearchBar的搜索功能进行编码。我计算出搜索结果并将其保存到
NSMutableArray*searchResults
。这看起来是这样的:

- (void) searchGrapesTableView {
    [searchResult removeAllObjects];
    numSearchWines=0;
    for (NSString *str in listOfWines)
    {
        NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
        if (titleResultsRange.length > 0) {
            [searchResult addObject:str];
            numSearchWines++;
        }
    }
}
listOfWines
也是一个NSMutableArray,其中包含可从中搜索的所有名称的列表。理想情况下,我希望确定
listOfWines
中具有匹配值的对象的索引,并将其添加到另一个NSMutableArray中,这样我以后就可以非常轻松地访问它。例如,稍后当我使用此搜索数据显示表格单元格时,我有以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"wineCell"];
    //determine how many to add to get the grape ID
    NSInteger grapeID=0;
    for (int i=0; i<indexPath.section; i++) {
        grapeID += [self.tableView numberOfRowsInSection:i];
    }

    Wines *currentWine;
    if (isSearchOn) {
        NSString *cellValue = [searchResult objectAtIndex:(grapeID+indexPath.row)];
        NSInteger index = 0;
        index = [listOfWines indexOfObject:cellValue];
        currentWine = [allWines objectAtIndex:(index)];
    }

    else {
        currentWine = [allWines objectAtIndex:(grapeID+indexPath.row)];
    }

    cell.textLabel.text = currentWine.name;
    return cell;
}

这听起来像indexesOfObjectsPassingTest:的作业。你应该在NSArray文档中查看。您可以这样使用它:

NSInteger theResultID = [searchResultID objectAtIndex:(grapeID+indexPath.row)];
currentWine = [allWines objectAtIndex:theResultID];
NSIndexSet  *indxs = [allWines indexesOfObjectsPassingTest: ^BOOL(NSString *obj, NSUInteger idx, BOOL *stop) {
        return [obj isEqualToString:searchBar.text]];
    }];

因此,这将循环遍历allWines中的所有值(我假设对象是字符串),并返回与搜索字符串匹配的任何对象的索引,为您提供一个包含找到的任何索引的索引集。

这听起来像是indexesOfObjectsPassingTest:的作业。你应该在NSArray文档中查看。您可以这样使用它:

NSInteger theResultID = [searchResultID objectAtIndex:(grapeID+indexPath.row)];
currentWine = [allWines objectAtIndex:theResultID];
NSIndexSet  *indxs = [allWines indexesOfObjectsPassingTest: ^BOOL(NSString *obj, NSUInteger idx, BOOL *stop) {
        return [obj isEqualToString:searchBar.text]];
    }];

因此,这将循环遍历allWines中的所有值(我假设对象是字符串),并返回与搜索字符串匹配的任何值的索引,为您提供一个包含找到的任何索引的索引集。

现在
[searchResult addObject:str]超出了for循环的范围。这可能是造成问题的原因吗?这是一个打字错误。我会编辑上面的。我想可能是。。。我现在是obj-c的新手,所以这是我唯一看错的;)别担心,我很感激你的帮助!现在
[searchResult addObject:str]超出了for循环的范围。这可能是造成问题的原因吗?这是一个打字错误。我会编辑上面的。我想可能是。。。我现在是obj-c的新手,所以这是我唯一看错的;)别担心,我很感激你的帮助!到目前为止,这听起来很棒。我要试试看。谢谢你的回答!我想知道是否有这样的事情存在!到目前为止,这听起来很棒。我要试试看。谢谢你的回答!我想知道是否有这样的事情存在!