搜索UISearchController

1.签协议,UISearchResultsUpdating,UISearchControllerDelegate

2.代码

#pragma mark - 搜索

- (UISearchController *)searchVC

{

if (!_searchVC) {

_searchVC = [[UISearchController alloc]initWithSearchResultsController:nil];

_searchVC.searchResultsUpdater = self;

_searchVC.dimsBackgroundDuringPresentation = NO;

_searchVC.hidesNavigationBarDuringPresentation = NO;

_searchVC.searchBar.placeholder = @"输入用户编码或者发起人";

[_searchVC.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];

_searchVC.searchBar.frame = CGRectMake(self.searchVC.searchBar.frame.origin.x, self.searchVC.searchBar.frame.origin.y, self.searchVC.searchBar.frame.size.width, 44.0);

self.compltedTableview.tableHeaderView = self.searchVC.searchBar;

}

return _searchVC;

}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

NSString *searchString = [self.searchVC.searchBar text];

NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];

NSMutableArray *guolvArr = [NSMutableArray array];

NSMutableArray *dicTaskCodeArr = [NSMutableArray array];

for (NSDictionary *littleDic in self.localDownloadListArray) {

[dicTaskCodeArr addObject:littleDic[@"taskCode"]];

[dicTaskCodeArr addObject:littleDic[@"creatorName"]];

}

self.searchArr = [NSMutableArray array];

//过滤数据

guolvArr= [NSMutableArray arrayWithArray:[dicTaskCodeArr filteredArrayUsingPredicate:preicate]];

NSLog(@"guolvArr  %@",guolvArr);

for (NSDictionary *littleDic  in self.localDownloadListArray) {

for (int i = 0 ; i < guolvArr.count;i++ ) {

if ([littleDic[@"taskCode"] isEqualToString:guolvArr[i]]){

//如果编码一致  则将该编码对应的字典存入search数组中

[self.searchArr addObject:littleDic];

}

}

if (guolvArr.count > 0 ) {

if ([littleDic[@"creatorName"] isEqualToString:guolvArr[0]]) {

[self.searchArr addObject:littleDic];

}

}

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if (self.searchVC.active) {

return self.searchArr.count;//搜索结果

}else

{

return self.localDownloadListArray.count;//原始数据

}

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

if (!self.searchVC.active) {

_searchDict = self.localDownloadListArray[indexPath.row];

}else

{

_searchDict = _searchArr[indexPath.row];

}

static NSString *CellIdentifier = @"Cell";

CheckCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[CheckCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

//设置点击的时候切换背景颜色

UIView *view_bg = [[UIView alloc]initWithFrame:cell.frame];

view_bg.backgroundColor = [ColorConvertUtil  colorWithHexString:@"#D5E4F2"];

cell.selectedBackgroundView = view_bg;

}

[cell setData:_searchDict];//给cell传数据

cell.index = indexPath.row;

NSMutableDictionary *dicte = _selettArray[indexPath.row];

NSString *isSelect = dicte[@"seleted"];

cell.isSeleted = [isSelect intValue];

__weak typeof(self) weakSelted = self;

//已选下标或者取消

cell.seletedBlock =^(NSString *index,BOOL isSeleted){

NSInteger inde =[index integerValue];

[weakSelted seleltIndex:inde isSeleted:isSeleted ];

};

return cell;

}

NSLog(@"searchArr = %@",self.searchArr);

//刷新表格

[self.compltedTableview reloadData];

}

你可能感兴趣的:(搜索UISearchController)