UI_搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)
来源:互联网 发布:js encodeuri编码 编辑:程序博客网 时间:2024/05/22 06:27
引言
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框.
而在之前版本中, 我们还是必须使用UISearchBar + UISearchDisplayController的组合方式.
添加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]; } }
0 0
- UI_搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController的组合)
- iOS - 搜索框UISearchController的使用(iOS8.0之后替代UISearchBar + UISearchDisplayController的组合)
- ios搜索框功能的实现/UISearchBar/UISearchController的使用
- UISearchController VS UISearchBar and UISearchDisplayController
- iOS--搜索UISearchController&&UISearchDisplayController
- 【学习ios之路:UI系列】(UISearchBar,UISearchDisplayController) 和UISearchController(iOS8新特性)
- UISearchBar和 UISearchDisplayController的使用
- UISearchBar和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar 和 UISearchDisplayController的使用
- UISearchBar和 UISearchDisplayController的使用
- 《基 于 N Gram 的无词典 中文分词算法》 n-gram读感
- C#中的类和var
- html5 canvas 粒子特效
- 【论文笔记】监控视频中异常事件检测及异常事件摘要
- C# JSON字符串序列化与反序列化
- UI_搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)
- Python配置第三方库Numpy和matplotlib的曲折之路
- Xcode调试UI
- 大数java来一波
- 14.5.1 Creating InnoDB Tables
- activiti中的流程变量
- shell 下数值、字符串比较
- eclipse出现乱码的情况
- <6>Android HAL 架构分析之硬件抽象层 hardware.h haraware.c sensors.c