SearchDisplayController search multiple arrays

时光毁灭记忆、已成空白 提交于 2019-12-02 03:03:47

The search display controller calls the

  UISearchDisplayDelegate 

method:

 searchDisplayController:shouldReloadTableForSearchString:

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.

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.

You can do that with NSPredicate using KVC object.

Create an NSObject respond to the KVC scheme http://theocacao.com/document.page/161 . You can use property for that.

Filter your array with an NSPredicate http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.name LIKE[cd] %@ OR self.alias LIKE[cd] %@",searchString,searchString];
NSArray *result = [baseArray filteredArrayUsingPredicate:predicate];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!