IOS之表视图添加搜索栏

时间:2021-03-24 07:13:26

下面是我们要实现的效果。本效果是在上一篇自定义表视图的基础上进行更改的。

IOS之表视图添加搜索栏   IOS之表视图添加搜索栏

1.将Search bar and search display拖动到ViewController中。不要添加Search Bar.

IOS之表视图添加搜索栏

2.修改ViewController的头文件

  1. #import <UIKit/UIKit.h>
  2. @interface IkrboyViewController4 : UIViewController
  3. {
  4. NSArray *dataArr;//用于显示表视图的数据
  5. NSArray *allDataArr;//存储所有数据,通过关键词搜索将搜索结果赋值给dataArr
  6. }
  7. @property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
  8. @end
#import <UIKit/UIKit.h>

@interface IkrboyViewController4 : UIViewController
{
NSArray *dataArr;//用于显示表视图的数据
NSArray *allDataArr;//存储所有数据,通过关键词搜索将搜索结果赋值给dataArr
}
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar; @end

3.修改自定义方法initTableViewData。将ScopeBar隐藏是考虑到iphone的显示高度问题。可自行决定。

  1. -(void)initTableViewData{
  2. NSBundle *bundle = [NSBundle mainBundle];
  3. NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];
  4. allDataArr = [[NSArray alloc] initWithContentsOfFile:plistPath];
  5. dataArr = [NSArray arrayWithArray:allDataArr];
  6. NSLog(@"table data count = %d",[allDataArr count]);
  7. //设定搜索栏ScopeBar隐藏
  8. [self.searchBar setShowsScopeBar:NO];
  9. [self.searchBar sizeToFit];
  10. }
-(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的三个事件触发

  1. //以下三个方法实现SearchBar的搜索功能
  2. //当文本内容发生改变时候,向表视图数据源发出重新加载消息
  3. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  4. {
  5. [self filterContentForSearchText:searchString scope:self.searchBar.selectedScopeButtonIndex];
  6. //YES情况下表视图可以重新加载
  7. return YES;
  8. }
  9. // 当Scope Bar选择发送变化时候,向表视图数据源发出重新加载消息
  10. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
  11. {
  12. [self filterContentForSearchText:self.searchBar.text scope:searchOption];
  13. // YES情况下表视图可以重新加载
  14. return YES;
  15. }
  16. //点击cancel按钮的事件
  17. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
  18. {
  19. //查询所有
  20. [self filterContentForSearchText:@"" scope:-1];
  21. }
//以下三个方法实现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.自定义关键词搜索功能

  1. //自定义搜索方法,根据关键词从allDataArr中搜索到满足搜索条件的元素,并将匹配的数组赋值给dataArr,由于dataArr是表视图的数据源,因此表视图的记录也会随之改变。
  2. - (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope;
  3. {
  4. if([searchText length]==0)
  5. {
  6. //查询所有
  7. dataArr = [NSArray arrayWithArray:allDataArr];
  8. NSLog(@"dataArr count = %d",[dataArr count]);
  9. return;
  10. }
  11. NSPredicate *scopePredicate;
  12. switch (scope) {
  13. case 0:
  14. scopePredicate = [NSPredicate predicateWithFormat:@"(SELF.itemName contains[c] %@) OR (SELF.itemImagePath contains[c] %@)",searchText,searchText];
  15. NSLog(@"searchText=%@",searchText);
  16. dataArr =[NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]];
  17. break;
  18. case 1:
  19. scopePredicate = [NSPredicate predicateWithFormat:@"SELF.itemName contains[c] %@",searchText];
  20. dataArr = [NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]];
  21. break;
  22. case 2:
  23. scopePredicate = [NSPredicate predicateWithFormat:@"SELF.itemImagePath contains[c] %@",searchText];
  24. dataArr =[NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]];
  25. break;
  26. }
  27. }
//自定义搜索方法,根据关键词从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方法

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. static NSString *CellIdentifier = @"myTableCell";
  4. MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  5. //add code begin:important,for showing searching results
  6. //不对cell进行空值的判断,会导致在搜索时,找不到对应identifier的cell而报错。
  7. if (cell == nil) {
  8. //搜索结果采用简单表视图cell的style,并非自定义的表视图cell的style
  9. cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  10. NSUInteger row = [indexPath row];
  11. NSDictionary *rowDict = [dataArr objectAtIndex:row];
  12. cell.textLabel.text =  [rowDict objectForKey:@"itemName"];
  13. NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];
  14. cell.imageView.image =  [UIImage imageNamed:imagePath];
  15. }
  16. //add code end
  17. NSUInteger row = [indexPath row];
  18. NSDictionary *rowDict = [dataArr objectAtIndex:row];
  19. cell.label.text =  [rowDict objectForKey:@"itemName"];
  20. NSLog(@"cell.label.text =  %@",[rowDict objectForKey:@"itemName"]);
  21. NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];
  22. cell.image.image = [UIImage imageNamed:imagePath];
  23. NSLog(@"cell.image.image  =  %@",imagePath);
  24. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  25. return cell;
  26. }
- (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;
}