使用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 *)searchController 108 { 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
时间: 2024-12-21 06:26:50