1

大家好,我开发了一个应用程序来搜索连接到 sqlite 数据库的表视图。应用程序中添加了一个搜索栏,搜索工作正常,但是当我键入时,我只需要出现以键入的字母开头的项目。这是我到目前为止的代码:

-(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];
}
4

1 回答 1

3

其实很简单。我只需要将选项文件从 options:NSCaseInsensitiveSearch 更改为 option:NSAnchoredSearch 它就像一个魅力

于 2012-07-20T06:14:55.713 回答