iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController的组合)
2015-07-14 21:04
841 查看
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISearchDisplayController的组合方式.
UISearchController的searchBar中的内容一旦发生变化, 就会调用该方法. 在其中, 我们可以使用NSPredicate来设置搜索过滤的条件.
这一点, 可以通过UISearchController的active属性来判断, 即判断输入框是否处于active状态.
UITableView相关的很多方法都要根据active来做判断:
添加UISearchController属性:
@property(strong, nonatomic) UISearchController *searchController; @property(strong, nonatomic) NSMutableArray *allCities; // 所有城市 @property(strong, nonatomic) NSMutableArray *filteredCities; // 根据searchController搜索的城市
UISearchController初始化
在viewDidLoad中初始化UISearchController:self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; self.searchController.searchResultsUpdater = self; self.searchController.dimsBackgroundDuringPresentation = false; [self.searchController.searchBar sizeToFit]; self.searchController.searchBar.backgroundColor = UIColorFromHex(0xdcdcdc); self.tableView.tableHeaderView = self.searchController.searchBar;
UISearchResultsUpdating协议
使用UISearchController要继承UISearchResultsUpdating协议, 实现其中的UISearchResultsUpdating方法.#pragma mark - searchController delegate - (void)updateSearchResultsForSearchController:(UISearchController *)searchController { [self.filteredCities removeAllObjects]; NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", self.searchController.searchBar.text]; self.filteredCities = [[self.allCities filteredArrayUsingPredicate:searchPredicate] mutableCopy]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); }
UISearchController的searchBar中的内容一旦发生变化, 就会调用该方法. 在其中, 我们可以使用NSPredicate来设置搜索过滤的条件.
更新UITableView
引入UISearchController之后, UITableView的内容也要做相应地变动: 即cell中要呈现的内容是allCities, 还是filteredCities.这一点, 可以通过UISearchController的active属性来判断, 即判断输入框是否处于active状态.
UITableView相关的很多方法都要根据active来做判断:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (!self.searchController.active) { return self.cityKeys.count; } else { return 1; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (!self.searchController.active) { NSString *key = self.cityKeys[section]; NSArray *citySection = self.cityDict[key]; return citySection.count; } else { return self.filteredCities.count; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } // 根据UISearchController的active属性来判断cell中的内容 if (!self.searchController.active) { NSString *key = self.cityKeys[indexPath.section]; cell.textLabel.text = [self.cityDict[key] objectAtIndex:indexPath.row]; } else { cell.textLabel.text = self.filteredCities[indexPath.row]; } return cell; }
UISearchController的移除
在viewWillDisappear中要将UISearchController移除, 否则切换到下一个View中, 搜索框仍然会有短暂的存在.- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if (self.searchController.active) { self.searchController.active = NO; [self.searchController.searchBar removeFromSuperview]; } }
相关文章推荐
- NSArray, NSNumber, NSValue, NSSet, 数组排序
- UI基础(二)-图层
- IOS开发UI篇--一个侧滑菜单SlidingMenu
- UI_UItableView_AutoCell(自定义cell 高度)
- UI中一些名词的缩写
- 暑假选拔赛02 ---- Ysequence
- The method setPositiveButton(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder i
- UI基础(一)
- String, StringBuilder , StringBuffer
- NGUI UIWrapContent小用法
- UITableView简单性能优化
- @DynamicUpdate(true) @DynamicInsert(true)使用的环境
- 算法导论 第七章:快速排序(Quicksort)
- Leetcode-N-Queens II
- Leetcode-N-Queens
- [LintCode] N-Queens
- 你用什么方法检查 PHP 脚本的执行效率(通常是脚本执行时间)和数据库 SQL 的效率(通常是数据库 Query 时间), 并定位和分析脚本执行和数据库查询的瓶颈所在?
- iOS发展系列II - UILabel 使用摘要
- 【设计和开发一套简单自动化UI框架】
- emulator: ERROR: x86 emulation currently requires hardware acceleration!Please ensure Intel HAXM is