当我在其中搜索 NSString 时,如何确定 NSMutableArray 中对象的索引? [英] How can I determine the index of an object in an NSMutableArray when I search for a NSString within it?

查看:53
本文介绍了当我在其中搜索 NSString 时,如何确定 NSMutableArray 中对象的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在使用表中显示的数据库中的数据编写 UISearchBar 的搜索功能.我找出搜索结果并将它们保存到 NSMutableArray *searchResults.这是它的外观:

Currently I am coding the search capability of a UISearchBar using data from a DB that's displayed in a table. I figure out the search results and save them to NSMutableArray *searchResults. Here's how that looks:

- (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,这样我以后就可以很容易地访问它.例如,稍后当我使用此搜索数据显示表格单元格时,我有以下代码:

listOfWines is also an NSMutableArray that contains a list of all the names that can be searched from. IDEALLY, I would like to determine the index of the object in listOfWines that has the matching value and add it to another NSMutableArray, so that way I can later access it really easily. For example, later on when I display the table cells using this search data, I have the following code:

- (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;
}

不幸的是,如果 listOfWines(以及随后的 searchResult)中有重复的条目,这将不起作用.这就是为什么将它们的索引也放在例如名为 searchResultID 的 NSMutableArray 中会如此有用,这样我就可以执行以下操作:

Unfortunately this doesn't work if there are duplicate entries in listOfWines (and subsequently searchResult). That's why it would be so useful to also have their indexes in, for example, an NSMutableArray named searchResultID, so that I can do something like this:

NSInteger theResultID = [searchResultID objectAtIndex:(grapeID+indexPath.row)];
currentWine = [allWines objectAtIndex:theResultID];

推荐答案

这听起来像是 indexOfObjectsPassingTest: 的工作.您应该在 NSArray 文档中查看它.您可以像这样使用它:

This sounds like a job for indexesOfObjectsPassingTest:. You should check it out in the NSArray docs. You use it something like this:

NSIndexSet  *indxs = [allWines indexesOfObjectsPassingTest: ^BOOL(NSString *obj, NSUInteger idx, BOOL *stop) {
        return [obj isEqualToString:searchBar.text]];
    }];

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

So this will loop through all the values in allWines (I'm assuming the objects are strings) and return the indexes of any that match the search string, giving you an index set with any indexes found.

这篇关于当我在其中搜索 NSString 时,如何确定 NSMutableArray 中对象的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆