Xcode : Search Bar Filtering

Discussion in 'iOS Programming' started by EliasRahme, Jul 18, 2012.

  1. EliasRahme, Jul 18, 2012
    Last edited: Jul 20, 2012

    EliasRahme macrumors newbie

    Jul 18, 2012
    Hy all, i have developed an application that searches a table view connected to a sqlite database. A search Bar is added onto of the application and the search is working fine, but when i type, i need only the items STARTING by the letter typed appear. This is my code till now :
    -(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
        if(text.length == 0)
            isFiltered = FALSE;
            isFiltered = true;
            filteredTableData = [[NSMutableArray alloc] init];
            for (Author* author in theauthors)
            {  //[NSPredicate predicateWithFormat:@"SELECT * from books where title LIKE %@", searchBar.text];
                NSRange nameRange = [author.name rangeOfString:text options:NSCaseInsensitiveSearch];
                NSRange descriptionRange = [author.genre rangeOfString:text options:NSCaseInsensitiveSearch];
                if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
                    [filteredTableData addObject:author];
        [self.tableView reloadData];
  2. PhoneyDeveloper macrumors 68030


    Sep 2, 2008
    The issue seems to be how you're doing your comparisons of the find string and your author.name. If you want the search to be anchored to the start of the author.name then you need to use a different comparison method. You could use one of the compare methods that takes a range or use the NSAnchoredSearch option, or hasPrefix (although that's probably not case insensitive). There are probably other options as well. It's just a matter of how you do your comparisons.

Share This Page