国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

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

2019-11-14 17:53:46
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

     

在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];  }}

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东丰县| 开阳县| 昔阳县| 德惠市| 阜新市| 岢岚县| 若尔盖县| 拜城县| 平定县| 林州市| 扶绥县| 罗平县| 麻城市| 宜昌市| 桐庐县| 阿尔山市| 河北区| 修水县| 桓仁| 嘉禾县| 偃师市| 屏东市| 汝城县| 富顺县| 时尚| 禹城市| 谢通门县| 南皮县| 高邑县| 沾益县| 宣恩县| 南丹县| 广东省| 安义县| 石嘴山市| 青川县| 新和县| 米泉市| 桃源县| 井陉县| 灵石县|