Xcode : Search Bar Filtering

这一生的挚爱 提交于 2019-12-08 06:43:09

问题


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;
    }
    else
    {
        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];
}

回答1:


It was actually pretty simple. I just had to change the options file from options:NSCaseInsensitiveSearch to option:NSAnchoredSearch It worked like a charm



来源:https://stackoverflow.com/questions/11517475/xcode-search-bar-filtering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!