objective-c - UISearchDisplayController 在多个数组中搜索

标签 objective-c ios uitableview searchdisplaycontroller

好吧,经过长时间的搜索、犹豫和更多的搜索,我似乎无法弄清楚这一点。

我有一个 SearchDisplayController,我有四个不同的数组,我在其中使用 NSPredicate 进行搜索,因为每个 UITableViewCell 由 4 个字符串(标题、描述等)组成。

我现在有 4 个搜索数组、search_title、search_description 等,它们是这样填充的:

- (void)filterContentForSearchText:(NSString*)searchText 
                             scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate 
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];

    self.search_category = [self.temp_category filteredArrayUsingPredicate:resultPredicate];
    self.search_producttitle = [self.price_producttitle filteredArrayUsingPredicate:resultPredicate];
    self.search_type = [self.price_type filteredArrayUsingPredicate:resultPredicate];
    self.search_description = [self.price_description filteredArrayUsingPredicate:resultPredicate];

当我使用 NSLog(数组计数)时,我可以看到它正在工作,因为当我输入搜索词时它会给出每个数组的正确计数。

我的表格 View 单元格如下所示:

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLabel.text = [self.search_producttitle objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
        cell.priceLabel.text = [self.search_price objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
        cell.descrLabel.text = [self.search_description objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
        cell.IDLabel.text = [self.search_type objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]];
    }
    else{
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLabel.text = [price_producttitle objectAtIndex:indexPath.row];
        cell.IDLabel.text = [price_type objectAtIndex:indexPath.row];
        cell.priceLabel.text = [price objectAtIndex:indexPath.row];
        cell.descrLabel.text = [price_description objectAtIndex:indexPath.row];

也许您已经知道我在做什么。我现在从旧数组创建新数组,我称之为搜索数组。然而,这些数组是相互独立的(一个数组可能是空的,而另一个数组充满了研究结果)。我需要知道数据来自哪个索引,存储在那些搜索数组中。如果我知道 search_producttitle 中的数据来自 price_producttitle(原始数组)的索引 1,3 和 4,那么我可以将这些数字用于 indexPath.row 来显示。至少,这是我目前的计划,制作一个 searchResult 数组,其中包含制作单元格时应在 objectAtIndex 中使用的数字。

我正在努力解决这个问题,我找不到任何在多个数组中搜索的示例,因为单元格由多个数组组成。

有人可以给我一个好的方向的提示,或者我可以使用的例子吗?

提前谢谢你。

普拉斯托

我使用的引用资料:

SearchDisplayController search multiple arrays

http://ygamretuta.me/2011/08/10/ios-implementing-a-basic-search-uisearchdisplaycontroller-and-interface-builder/

最佳答案

虽然没有人回答我的问题,但我自己想通了。

其实我想的太难了。我想使用 NSPredicate,这对我来说不是必需的。我想先从 NSPredicate 得到一个字符串,然后比较这个字符串。我现在使用 rangeofstring,在每个 objectatindex 的循环中,我对每个数组都这样做。

如果在数组中找到,则报告 YES,索引将被放入另一个数组中。该数组将在稍后制作 UITableView 时使用,替换 indexPath.row。

[self.searchResults removeAllObjects];

for (int i = 0; i < [temp_category count]; i++) {
    BOOL foundResult = FALSE;

    if ([[temp_category objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
        foundResult = TRUE;
    }
    if ([[price_producttitle objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
        foundResult = TRUE;
    }
    if ([[price_type objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
        foundResult = TRUE;
    }
    if ([[price_description objectAtIndex:i] rangeOfString:searchText].location != NSNotFound) {
        foundResult = TRUE;
    }
    if (foundResult) {

        NSNumber *result = [NSNumber numberWithInt:i];
        if ([self searchResults] == nil) {
            NSMutableArray *array = [[NSMutableArray alloc] init];
            [self setSearchResults:array];
            [array release];
        }

            [searchResults addObject:result];

    }
}

NSLog (@"array = %i", [searchResults count]);
NSLog(@"%@",searchResults);

我不把这个答案归功于我。学分应转至:SearchDisplayController search multiple arrays

我采用了这种方法并在 -(void)searchtableview 中实现了它。我应该提一下,关于这个的问题并不多,这是我能找到的唯一合适的答案。

我希望其他人也会觉得这很有用!!

关于objective-c - UISearchDisplayController 在多个数组中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11365453/

相关文章:

objective-c - NSView-dataWithPDFInsideRect : and layer borders

ios - 搜索栏不显示结果

iphone - 动画时停止滚动表格 View

objective-c - 有人可以向我解释为什么我看到的某些代码在函数中嵌入了函数吗?

iphone - UIBarButtonSystemItem 可本地化

iphone - 设备总下载和上传数据

ios - 我在解析 XML 时显示一条错误消息

android - 为什么推送通知服务器准确地向客户端发送消息

ios - 如何在TableViewController上给TableView添加约束?

iPhone:自定义 UITableViewCell 具有多个响应点击的区域?