SearchDisplayController搜索多个数组 [英] SearchDisplayController search multiple arrays

查看:56
本文介绍了SearchDisplayController搜索多个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在用代表名称,id等的多个数组的内容填充tableviewcells.

Currently I'm populating my tableviewcells with the contents of multiple arrays representing a name, id, etc.

当我开始使用搜索显示控制器时,我的问题就来了.我有一个数组,其中包含名称列表,ID列表,条形码列表和别名列表.当用户在搜索栏中键入内容时,我需要能够搜索所有4个数组.当它在1个数组中找到结果时,必须将结果与其他3个数组配对.

My question comes when I start to use the search display controller. I have an array with a list of names, a list of IDs, a list of barcodes, and a list of Aliases. When the user types in the search bar I need to be able to search all 4 arrays. When it finds the result in 1 array it has to pair the result with the 3 other arrays..

示例

  1. 名字(苹果,胡萝卜,香蕉,狗)
  2. 别名(红色,橙色,黄色,棕色)
  3. 条形码(1,2,10,20)
  4. id(30,40,50,60)

因此,如果用户键入"a",则应在表视图中填充 苹果,胡萝卜,香蕉和相关的别名,条形码,ID.

So if the user types "a" I should populate the table view with Apple, Carrot, Banana and the associated alias, barcode, id.

如果用户输入2,我应该只得到 胡萝卜和狗.

If the user were to type 2 I should only get carrot and dog.

如果用户输入0,我将得到所有这些项目.

If the user were to type 0 I would get all of those items.

有什么想法可以做到这一点吗?

Any ideas how to accomplish this?

更新: 这就是我的方法.

UPDATE: This is how I did it.

    -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    BOOL shouldReturn = FALSE;
    [searchResults removeAllObjects];

    for (int i = 0; i < [itemIDRows count]; i++) {

        BOOL foundResult = FALSE;

        if ([[itemIDRows objectAtIndex:i] rangeOfString:searchString].location != NSNotFound) {
            foundResult = TRUE;
        }
        if ([[nameRows objectAtIndex:i] rangeOfString:searchString].location != NSNotFound) {
            foundResult = TRUE;
        }
        if ([[barcodeRows objectAtIndex:i] rangeOfString:searchString].location != NSNotFound) {
            foundResult = TRUE;
        }
        if ([[aliasRows objectAtIndex:i] rangeOfString:searchString].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];
            shouldReturn = YES;
        }
    }
    return shouldReturn;    
}

然后,当我填充表格视图时,我会做类似的事情

Then when I'm populating the tableview I do something like this

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) { 
    [cell setCellContentsName:[NSString stringWithFormat:@"%@", [nameRows objectAtIndex:[[searchResults objectAtIndex:indexPath.row] integerValue]]];  
} else {
    [cell setCellContentsName:[NSString stringWithFormat:@"%@", [nameRows objectAtIndex:indexPath.row]];
}

但是,当我输入9999之类的内容时,它会弹出实例,其中ID或条形码中只有1 9.有任何解决办法的想法吗?

However when I type something like 9999 it brings up instances where only 1 9 is in the ID or barcode. Any ideas how to fix that?

UPDATE2: 通过使列表始终刷新而不是仅在发现结果后才重新加载数据来解决此问题.现在它可以完美运行了:D

UPDATE2: Solved the problem by having the list always refresh instead of only reloading the data if a result was found. Now it works perfectly :D

推荐答案

搜索显示控制器调用

  UISearchDisplayDelegate 

方法:

 searchDisplayController:shouldReloadTableForSearchString:

在此方法内,您需要实现逻辑.此逻辑将需要在您的所有4个数组中搜索匹配项,并进行适当的查找(即从橙色变成胡萝卜,或者从50变成香蕉).每次您遇到问题时,我都会将其放入NSMutableSet中(以防止重复).然后,当您搜索完所有数组后,将其复制到表数据源读取的数组中.

Inside this method, you need to implement your logic. This logic will need to search all 4 of your arrays for hits, and do the appropriate lookups (i.e. to get from orange to carrot, or from 50 to banana). Each time you get a hit, I would put it in an NSMutableSet (to prevent dupes). Then when you're done searching all arrays, copy the set into the array that your table's data source reads from.

如果如果要向用户显示为什么给定行被点击(例如,他们输入50并得到了香蕉),则必须在表格单元格中显示所有4个属性.并且您需要突出显示匹配的部分.如果执行此操作,我将创建一个小的容器类,例如包含所有4个属性的"searchHit"类,以及包含获得该命中的属性的标志,以及可能包含获得该命中的属性的子字符串(因此您可以为该子字符串使用黄色背景.)然后,tableView的数据源将显示这些searchHit对象的数组,而您的cellForRowAtIndexPath将需要对该对象进行解码并适当地显示匹配.

If you want to show the user WHY a given row is a hit (i.e. they typed 50 and got banana), you'd have to display all 4 of the attributes in your table cell. And you'd need to highlight the part that matched. If you do this, I'd create a small container class, something like "searchHit" that contains all 4 attributes, as well as a flag for which attribute got the hit, and possibly the substring of the attribute that got the hit (so you can use a yellow background for this substring, for example.) The tableView's data source would then have an array of these searchHit objects to display, and your cellForRowAtIndexPath would need to decode this object and display the hit appropriately.

这篇关于SearchDisplayController搜索多个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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