標籤:
使用UISearchController 配合UITableView實現搜尋功能
1 #import "ViewController12.h" 2 3 @interface ViewController12 () <UITableViewDataSource , UITableViewDelegate , UISearchResultsUpdating> 4 5 @end 6 7 @implementation ViewController12 8 { 9 UISearchController * _searchC;//SearchController 10 UITableView *_tableV; //tableView 11 NSMutableArray * _selectArr;//存放搜尋結果數組 12 NSMutableArray *_dataArr;//存放所有資料的數組 13 } 14 - (void)viewDidLoad 15 { 16 [super viewDidLoad]; 17 self.view.backgroundColor = [UIColor whiteColor]; 18 //是否根據按所在介面的navigationbar與tabbar的高度,自動調整scrollview的 inset,設定為no,讓它不要自動調整就 19 self.automaticallyAdjustsScrollViewInsets = NO; 20 [self createData]; 21 [self createTableView]; 22 _selectArr = [[NSMutableArray alloc] init]; 23 } 24 25 26 #pragma mark 建立資料 27 -(void)createData 28 { 29 if (!_dataArr) 30 { 31 _dataArr = [[NSMutableArray alloc] init]; 32 } 33 34 for (NSInteger i = 0; i<100; i++) 35 { 36 [_dataArr addObject:[NSString stringWithFormat:@"%ld",i]]; 37 } 38 NSLog(@"%@",_dataArr); 39 } 40 41 #pragma mark 建立 TableView UISearchController 42 -(void)createTableView 43 { 44 _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64-54)]; 45 46 _tableV.delegate = self; 47 _tableV.dataSource = self; 48 _searchC = [[UISearchController alloc] initWithSearchResultsController:nil]; 49 50 51 _searchC.hidesNavigationBarDuringPresentation = NO; 52 _searchC.dimsBackgroundDuringPresentation = YES; 53 //設定代理 54 _searchC.searchResultsUpdater = self; 55 //調整SearchBar尺寸為自適應 56 [_searchC.searchBar sizeToFit]; 57 //把SearchBar 給 TableView的標題 58 _tableV.tableHeaderView = _searchC.searchBar; 59 [self.view addSubview:_tableV]; 60 61 } 62 63 64 65 66 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 67 { 68 return 1; 69 } 70 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 71 { 72 //通過active屬性判斷是否搜尋 73 if (_searchC.active) 74 { 75 return _selectArr.count; 76 } 77 else 78 return _dataArr.count; 79 } 80 81 //設定儲存格 cell 82 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 83 { 84 static NSString * str = @"cell"; 85 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str]; 86 if (!cell) 87 { 88 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str]; 89 } 90 91 if (_searchC.active) 92 { 93 cell.textLabel.text = _selectArr[indexPath.row]; 94 } 95 else 96 { 97 cell.textLabel.text = _dataArr[indexPath.row]; 98 } 99 return cell;100 }101 102 103 104 105 106 //執行搜尋107 -(void)updateSearchResultsForSearchController:(UISearchController *)searchController108 {109 NSString *searchString = [_searchC.searchBar text];110 NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];111 if (_selectArr!= nil) {112 [_selectArr removeAllObjects];113 }114 //過濾資料115 _selectArr= [NSMutableArray arrayWithArray:[_dataArr filteredArrayUsingPredicate:preicate]];116 //重新整理表格117 [_tableV reloadData];118 }119 120 121 122 - (void)didReceiveMemoryWarning {123 [super didReceiveMemoryWarning];124 // Dispose of any resources that can be recreated.125 }126 127 @end
IOS UI學習 UISearchController