xcode - How do I make UISearchBar select from the filtered results? -
i hope can me. have search bar implemented , filter results way it's supposed to. problem when select filtered results, selects original array. i've read, 1 approach remove items filtered array , add selected items. other approach remove items don't match search criteria.
the problem don't know how - despite hours of searching. grateful or pointers in right direction.
here code:
(void)viewdidload { [super viewdidload]; self.tableview.scrollenabled = yes; categories = [nsarray arraywithobjects: @"other", @"breakfast", @"chemist", @"computer", @"dinner", @"drinks", @"entertainment", @"fuel", @"groceries", @"haircut", @"hotel", @"internet", @"laundry", @"lunch", @"meals", @"medical", @"parking", @"snacks", @"stationery", @"taxis", @"telephone", @"transport", @"travel taxes", nil]; self.allcatgeories = categories; [self.tableview reloaddata]; }
and
(void)filtercontentforsearchtext:(nsstring*)searchtext scope:(nsstring*)scope { nspredicate *resultpredicate = [nspredicate predicatewithformat:@"self contains[cd] %@", searchtext]; self.searchresults = [self.allcatgeories filteredarrayusingpredicate:resultpredicate]; }
you can use below, works fine me:
- (void)filtercontentforsearchtext:(nsstring*)searchtext scope:(nsstring*)scope { [self.filteredinboxitems removeallobjects]; /* search main list products type matches scope (if selected) , name matches searchtext; add items match filtered array. */ (nsnumber *id in self.inboxitemdao.inboxitemids) { nsdictionary *inboxitem = [self.inboxitemdao getinboxitem:id]; if ([scope isequaltostring:@"bla bla 1"]) { nscomparisonresult result = [[inboxitem objectforkey:@"subject"] compare:searchtext options:(nscaseinsensitivesearch|nsdiacriticinsensitivesearch) range:nsmakerange(0, [searchtext length])]; if (result == nsorderedsame) { [self.filteredinboxitems addobject:inboxitem]; } } else if ([scope isequaltostring:@"bla bla2"]) { nscomparisonresult result = [[inboxitem objectforkey:@"sender"] compare:searchtext options:(nscaseinsensitivesearch|nsdiacriticinsensitivesearch) range:nsmakerange(0, [searchtext length])]; if (result == nsorderedsame) { [self.filteredinboxitems addobject:inboxitem]; } } else if ([scope isequaltostring:@"bla bla 3"]) { nscomparisonresult result = [[inboxitem objectforkey:@"action"] compare:searchtext options:(nscaseinsensitivesearch|nsdiacriticinsensitivesearch) range:nsmakerange(0, [searchtext length])]; if (result == nsorderedsame) { [self.filteredinboxitems addobject:inboxitem]; } } }
}
Comments
Post a Comment