iOS --- 搜索框UISearchController的使用(iOS8.0之後替代UISearchBar+display)

     

在iOS 8.0以上版本中, 咱們能夠使用UISearchController來很是方便地在UITableView中添加搜索框. 而在以前版本中, 咱們仍是必須使用UISearchBar + UISearchDisplayController的組合方式.php

添加UISearchController屬性:app

@property(strong, nonatomic) UISearchController *searchController; @property(strong, nonatomic) NSMutableArray *allCities; // 全部城市 @property(strong, nonatomic) NSMutableArray *filteredCities; // 根據searchController搜索的城市

UISearchController初始化async

在viewDidLoad中初始化UISearchController:ui

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協議atom

使用UISearchController要繼承UISearchResultsUpdating協議, 實現其中的UISearchResultsUpdating方法.url

#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來設置搜索過濾的條件.spa

更新UITableViewcode

引入UISearchController以後, UITableView的內容也要作相應地變更: 即cell中要呈現的內容是allCities, 仍是filteredCities. 
這一點, 能夠經過UISearchController的active屬性來判斷, 即判斷輸入框是否處於active狀態. 
UITableView相關的不少方法都要根據active來作判斷:orm

- (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]; } }
相關文章
相關標籤/搜索