標籤:style blog http color os 使用 io for 資料
自己實現 UITableView 搜尋,相對於使用 UISearchDisplayController 來說自己寫稍微麻煩了那麼一點點,但是更加靈活。主要就是用一個欄位區分出當前是搜尋還是非搜尋,然後 reload 相應的 data 就行了,和 UISearchDisplayController 的實現也很像,不過 UISearchDisplayController是兩個 tableview 切換,這裡我們是一個 tableview load 不同的資料。
關鍵代碼:
@interface MainTableViewController : UIViewController<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>{ UITableView *mytableView; NSArray *data; NSMutableArray *filterData; BOOL isFiltered; // 標識是否正在搜素 UIView *mask;}
- (void)viewDidLoad{ [super viewDidLoad]; mytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20)]; mytableView.dataSource = self; mytableView.delegate = self; [self.view addSubview:mytableView]; UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , 44)]; searchBar.placeholder = @"搜尋"; searchBar.delegate = self; mytableView.tableHeaderView = searchBar; // 添加一層 mask mask = [[UIView alloc] initWithFrame:CGRectMake(0, 20 + 44, self.view.frame.size.width, self.view.frame.size.height -20 -44)]; [self.view addSubview:mask]; mask.backgroundColor = [UIColor blackColor]; mask.alpha = 0;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // 通過 isFiltered 區分出當前顯示的是搜尋結果集還是原結果集 if (isFiltered) { return filterData.count; } return data.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellid = @"cellid"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid]; if (cell==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid]; } // 通過 isFiltered 區分出當前顯示的是搜尋結果集還是原結果集 if (isFiltered) { cell.textLabel.text = filterData[indexPath.row]; }else{ cell.textLabel.text = data[indexPath.row]; } return cell;}- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ // 開始搜尋時彈出 mask 並禁止 tableview 點擊 NSLog(@"searchBarTextDidBeginEditing"); isFiltered = YES; searchBar.showsCancelButton = YES; mask.alpha = 0.3; mytableView.allowsSelection = NO; mytableView.scrollEnabled = NO;}- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{ NSLog(@"searchBarTextDidEndEditing");}- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ NSLog(@"textDidChange"); if (searchText.length == 0) { isFiltered = NO; mask.alpha = 0.3; mytableView.allowsSelection = NO; mytableView.scrollEnabled = NO; [mytableView reloadData]; return; } isFiltered = YES; mask.alpha = 0; mytableView.allowsSelection = YES; mytableView.scrollEnabled = YES; // 謂詞搜尋 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@",searchText]; filterData = [[NSMutableArray alloc] initWithArray:[data filteredArrayUsingPredicate:predicate]]; [mytableView reloadData];}- (void)searchBarCancelButtonClicked:(UISearchBar *) sb{ // 點擊 cancel 時去掉 mask ,reloadData sb.text = @""; [sb setShowsCancelButton:NO animated:YES]; mytableView.allowsSelection = YES; mytableView.scrollEnabled = YES; [sb resignFirstResponder]; mask.alpha = 0; isFiltered = NO; [mytableView reloadData];}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *text; if (isFiltered) { text = filterData[indexPath.row]; }else{ text = data[indexPath.row]; } NSLog(@"you click index:%d %@",indexPath.row,text);}
基於 ios7.1 布局寫了一個 demo: http://pan.baidu.com/s/1ntn0MeP
ios UITableView 搜尋