SearchDisplayController search multiple arrays

后端 未结 2 1690
情话喂你
情话喂你 2021-01-25 10:42

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

My question comes when I start to use the search display c

相关标签:
2条回答
  • 2021-01-25 11:18

    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.

    0 讨论(0)
  • 2021-01-25 11:19

    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];
    
    0 讨论(0)
提交回复
热议问题