下面是我们要实现的效果。本效果是在上一篇自定义表视图的基础上进行更改的。
1.将Search bar and search display拖动到ViewController中。不要添加Search Bar.
2.修改ViewController的头文件
Cpp代码 class="star" src="/Upload/Images/2014122614/40B102E0EF997EA6.png" alt="收藏代码" />spinner" style="display: none;" src="/Upload/Images/2014122614/4E072B8B8C20032D.gif" alt="" />#import <UIKit/UIKit.h> @interface IkrboyViewController4 : UIViewController { NSArray *dataArr;//用于显示表视图的数据 NSArray *allDataArr;//存储所有数据,通过关键词搜索将搜索结果赋值给dataArr } @property (weak, nonatomic) IBOutlet UISearchBar *searchBar; @end
3.修改自定义方法initTableViewData。将ScopeBar隐藏是考虑到iphone的显示高度问题。可自行决定。
Cpp代码-(void)initTableViewData{ NSBundle *bundle = [NSBundle mainBundle]; NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"]; allDataArr = [[NSArray alloc] initWithContentsOfFile:plistPath]; dataArr = [NSArray arrayWithArray:allDataArr]; NSLog(@"table data count = %d",[allDataArr count]); //设定搜索栏ScopeBar隐藏 [self.searchBar setShowsScopeBar:NO]; [self.searchBar sizeToFit]; }
4.添加SearchBar的三个事件触发
Cpp代码//以下三个方法实现SearchBar的搜索功能 //当文本内容发生改变时候,向表视图数据源发出重新加载消息 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { [self filterContentForSearchText:searchString scope:self.searchBar.selectedScopeButtonIndex]; //YES情况下表视图可以重新加载 return YES; } // 当Scope Bar选择发送变化时候,向表视图数据源发出重新加载消息 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { [self filterContentForSearchText:self.searchBar.text scope:searchOption]; // YES情况下表视图可以重新加载 return YES; } //点击cancel按钮的事件 - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { //查询所有 [self filterContentForSearchText:@"" scope:-1]; }
5.自定义关键词搜索功能
Cpp代码//自定义搜索方法,根据关键词从allDataArr中搜索到满足搜索条件的元素,并将匹配的数组赋值给dataArr,由于dataArr是表视图的数据源,因此表视图的记录也会随之改变。 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope; { if([searchText length]==0) { //查询所有 dataArr = [NSArray arrayWithArray:allDataArr]; NSLog(@"dataArr count = %d",[dataArr count]); return; } NSPredicate *scopePredicate; switch (scope) { case 0: scopePredicate = [NSPredicate predicateWithFormat:@"(SELF.itemName contains[c] %@) OR (SELF.itemImagePath contains[c] %@)",searchText,searchText]; NSLog(@"searchText=%@",searchText); dataArr =[NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]]; break; case 1: scopePredicate = [NSPredicate predicateWithFormat:@"SELF.itemName contains[c] %@",searchText]; dataArr = [NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]]; break; case 2: scopePredicate = [NSPredicate predicateWithFormat:@"SELF.itemImagePath contains[c] %@",searchText]; dataArr =[NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]]; break; } }
6.修改cellForRowAtIndexPath方法
Cpp代码- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"myTableCell"; MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //add code begin:important,for showing searching results //不对cell进行空值的判断,会导致在搜索时,找不到对应identifier的cell而报错。 if (cell == nil) { //搜索结果采用简单表视图cell的style,并非自定义的表视图cell的style cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; NSUInteger row = [indexPath row]; NSDictionary *rowDict = [dataArr objectAtIndex:row]; cell.textLabel.text = [rowDict objectForKey:@"itemName"]; NSString *imagePath = [rowDict objectForKey:@"itemImagePath"]; cell.imageView.image = [UIImage imageNamed:imagePath]; } //add code end NSUInteger row = [indexPath row]; NSDictionary *rowDict = [dataArr objectAtIndex:row]; cell.label.text = [rowDict objectForKey:@"itemName"]; NSLog(@"cell.label.text = %@",[rowDict objectForKey:@"itemName"]); NSString *imagePath = [rowDict objectForKey:@"itemImagePath"]; cell.image.image = [UIImage imageNamed:imagePath]; NSLog(@"cell.image.image = %@",imagePath); cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; }