I have implemented the search method for a UITableView populate from a NSArray (mylist):
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
// reset array
[self.filteredListContent removeAllObjects];
// check the elements contained in the list
NSString *cellTitle;
for (cellTitle in myList){ //CHANGE HERE
NSComparisonResult result = [cellTitle compare:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame){
[filteredListContent addObject:cellTitle];
}
}
}
Now i would the same method for search a char in objectForKey:@"name" of a List of NSDictionary:
myList [0]:
{
gender
id
name
picture }
myList [1]
{
gender
id
name
picture }
I would something like this:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
[self.filteredFriendsList removeAllObjects];
NSString *cellTitle;
for (cellTitle in [[friendsList /* all objects */] objectForKey:@"name"]){
NSComparisonResult result = [cellTitle compare:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame){
[filteredFriendsList addObject:cellTitle];
}
}
}
Someone has some ideas?
Thank you.
Try this one:
The key here is the fact that
valueForKey:method once invoked on anNSArrayit callsvalueForKey:on every of its objects.Ps. If you want a case insensitive comparison you can do so like:
EDIT: (To include solution for substring matching)