Xcode:搜索栏过滤 [英] Xcode : Search Bar Filtering

查看:25
本文介绍了Xcode:搜索栏过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

这篇关于Xcode:搜索栏过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆