iOS搜索功能(search demo)

本文介绍了一种iOS平台上的搜索功能实现方法,通过使用NSPredicate进行数据过滤,支持大小写及发音符号忽略等特性,并提供了实时搜索功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] %@",searchController.searchBar.text];

//  数组提供的快速遍历,返回的类型是NSArray

NSLog(@"%@",[ _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

self.dataList=[NSMutableArray arrayWithCapacity:1000]; for (NSInteger i=0; i<1000; i++) { [self.dataList addObject:[NSString stringWithFormat:@"010000----201200%ld",(long)i]]; } // Do any additional setup after loading the view from its nib. } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (tableView == self.searchDisplayController.searchResultsTableView) { return [self.searchList count]; }else{ return [self.dataList count]; } } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *flag=@"cellFlag"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag]; } if (tableView==self.searchDisplayController.searchResultsTableView) { [cell.textLabel setText:self.searchList[indexPath.row]]; } else{ [cell.textLabel setText:self.dataList[indexPath.row]]; } return cell; } //UISearchBarDelegate中德开始和结束的事件: - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ NSLog(@"搜索Begin"); return YES; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{ NSLog(@"搜索End"); return YES; } - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{ // 谓词的包含语法 NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString]; if (self.searchList!= nil) { [self.searchList removeAllObjects]; } //过滤数据 self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]]; //刷新表格 return YES; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值