1

answered thanks to all that helped. I'm new to stack overflow as far as posting so if my edit is messy i apologize. the code below is what i ended up coming up with and think it may help others though i believe the person who answered last may also produce similar results.

I have been searching for a while now and I am sure I could break this down and get it done with a couple of sloppy if statements but I am looking for a common, efficient way.

I have a search with typical string comparison. For each string in an array using NSRange, but the results I get from that can be picky.

If I have a string for example Bat Man I could find it with searching Bat or Man but not once I add an m onto bat for Batm.

I guess what im looking for, is if the characters exist within the string and not exactly that string.

     -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{


[roomsList removeAllObjects];
stringScore = 0;


// clear
if([searchText length] == 0){

    searchingRooms = NO;

    [roomsList addObjectsFromArray:[newDataManager shared].rooms];
    [self.gsgSearchBar resignFirstResponder];

    // search users
}else{



    searchingRooms = YES;

  for(QBChatRoom *room in [newDataManager shared].rooms){
      stringScore = 0;
      NSString*   newRoomNameText = [room.name stringByReplacingOccurrencesOfString:@"_" withString:@""];

      NSString* newSearchText = [searchText stringByReplacingOccurrencesOfString:@" " withString:@""];




      for(int i = 0 ;i<[newSearchText length]; i++) {
          char mySearchChar = [newSearchText characterAtIndex:i];

          lastSearchString = [NSString stringWithFormat:@"%c",mySearchChar];

          NSRange searchRange = [newRoomNameText rangeOfString:lastSearchString options:NSCaseInsensitiveSearch];
          if (searchRange.location != NSNotFound){
              stringScore = stringScore + 1;
              if (stringScore == newSearchText.length) {
                  [roomsList addObject:room];

              }



          }}}}


[gsgTableView reloadData];
}
4

1 回答 1

0

您可以使用谓词来解决这个问题。它们在过滤数组时非常方便。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Self contains[c] %@",self.searchString.text];

NSArray *filteredArray = [self.superHeroesArray filteredArrayUsingPredicate:predicate];

添加[c]aftercontains确保搜索不区分大小写。

于 2013-04-22T02:28:24.483 回答