在iOS 8.0以上版本中, 我們可以使用UISearchController來(lái)非常方便地在UITableView中添加搜索框. 而在之前版本中, 我們還是必須使用UISearchBar + UISearchDisplayController的組合方式.
添加UISearchController屬性:
@PRoperty(strong, nonatomic) UISearchController *searchController;@property(strong, nonatomic) NSMutableArray *allCities; // 所有城市@property(strong, nonatomic) NSMutableArray *filteredCities; // 根據(jù)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協(xié)議
使用UISearchController要繼承UISearchResultsUpdating協(xié)議, 實(shí)現(xiàn)其中的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中的內(nèi)容一旦發(fā)生變化, 就會(huì)調(diào)用該方法. 在其中, 我們可以使用NSPredicate來(lái)設(shè)置搜索過(guò)濾的條件.
更新UITableView
引入U(xiǎn)ISearchController之后, UITableView的內(nèi)容也要做相應(yīng)地變動(dòng): 即cell中要呈現(xiàn)的內(nèi)容是allCities, 還是filteredCities.
這一點(diǎn), 可以通過(guò)UISearchController的active屬性來(lái)判斷, 即判斷輸入框是否處于active狀態(tài).
UITableView相關(guān)的很多方法都要根據(jù)active來(lái)做判斷:
- (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; } // 根據(jù)UISearchController的active屬性來(lái)判斷cell中的內(nèi)容 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移除, 否則切換到下一個(gè)View中, 搜索框仍然會(huì)有短暫的存在.
- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if (self.searchController.active) { self.searchController.active = NO; [self.searchController.searchBar removeFromSuperview]; }}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注