iOS搜索功能(search demo)

来源:互联网 发布:美辰堂燕窝是假货 知乎 编辑:程序博客网 时间:2024/05/29 13:34

这段时间做了一下搜索功能,网上搜了搜各式各样,因此根据需求写了个demo,以便满足现在项目的需求。

搜索功能,顾名思义就是为了搜索内容,那么我们的关注点就有了——内容查找(注释:这个查找可能是本地数据,也有可能是网络数据)

关键点代码 (查找内容) // 方法一:([c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。)

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] %@",searchController.searchBar.text];//  数组提供的快速遍历,返回的类型是NSArrayNSLog(@"%@",[ _searchResults filteredArrayUsingPredicate:predicate]);// 方法二:for (int i = 0; i < _searchResults.count; i++) {    if ([predicate evaluateWithObject:[ _searchResults objectAtIndex:i]]) {        NSLog(@"%@",[_searchResults objectAtIndex:i]);    }}

关键代码 实时搜索: - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

// 变化后的字符串NSString * new_text_str = [textField.text stringByReplacingCharactersInRange:range withString:string];// 变化后的字符串只要发送变化就打印,这里也可以实时网络请求if (new_text_str.length > 0) {    NSLog(@"%@",new_text_str);}

下载链接:https://github.com/FlyJing/SearchFunction.git

0 0
原创粉丝点击