iOS UISearchController的使用

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setTabBarHidden:YES withAnimation:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.mySearchController.searchBar setHidden:YES];
    [self setTabBarHidden:NO withAnimation:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setExtraCellLineHidden:self.myTableViewW
     ];
    [self initCellInfo];
    [self initTableView];
    [self initTitleBar];
    [self initHeader];
    [self initSearchBar];
}
- (void)initSearchBar{
    self.mySearchController=[[UISearchController alloc]initWithSearchResultsController:nil];
    self.mySearchController.searchBar.frame=CGRectMake(100, 10, 200, 44);
    self.myTableViewW.tableHeaderView=self.mySearchController.searchBar;
    [self.mySearchController.searchBar sizeToFit];
    self.mySearchController.searchResultsUpdater=self;
    self.mySearchController.dimsBackgroundDuringPresentation=NO;
    self.mySearchController.hidesNavigationBarDuringPresentation=NO;
    self.mySearchController.searchBar.placeholder=@"输入赤兔ID或者昵称查询";
    [self presentViewController:self.mySearchController animated:YES completion:nil];
}
- (void)initHeader{
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
   [self hiddenkeyBoard];
}
- (void)hiddenkeyBoard{
    [self.myTextField resignFirstResponder];
}
- (void)initTitleBar{
    self.title=@"添加朋友";
}
- (void)initCellInfo{
    NSMutableArray *cellInfoArray=[[NSMutableArray alloc]init];
    NSMutableArray *nickNameArray=[[NSMutableArray alloc]init];
    NSMutableArray *IDArray=[[NSMutableArray alloc]init];
    CellInfo_t *cellInfo=[self newCellInfo];
    cellInfo.image=@"http://b.hiphotos.baidu.com/image/w%3D310/sign=a0eb5ed6d52a60595210e71b1835342d/2fdda3cc7cd98d10f00dbe2e233fb80e7aec90d0.jpg";
    cellInfo.nickName=@"呱呱呱";
    cellInfo.ID=@"1";
    [cellInfoArray addObject:cellInfo];
    [nickNameArray addObject:cellInfo.nickName];
    [IDArray addObject:cellInfo.ID];
    
    cellInfo=[self newCellInfo];
    cellInfo.image=@"http://c.hiphotos.baidu.com/image/w%3D310/sign=4632f6f379cb0a4685228d385b62f63e/902397dda144ad341343938ed2a20cf431ad853e.jpg";
    cellInfo.nickName=@"任溶溶";
    cellInfo.ID=@"2";
    [cellInfoArray addObject:cellInfo];
    [nickNameArray addObject:cellInfo.nickName];
    [IDArray addObject:cellInfo.ID];
    
    cellInfo=[self newCellInfo];
    cellInfo.image=@"http://tao.goulew.com/users/upfile/201403/20140305222531big.jpg";
    cellInfo.nickName=@"小安江湖";
    cellInfo.ID=@"3";
    [cellInfoArray addObject:cellInfo];
    [nickNameArray addObject:cellInfo.nickName];
    [IDArray addObject:cellInfo.ID];
    
    cellInfo=[self newCellInfo];
    cellInfo.image=@"http://d.hiphotos.baidu.com/image/w%3D310/sign=4276550c5e6034a829e2be80fb1249d9/d000baa1cd11728b9c43502fcafcc3cec3fd2c55.jpg";
    cellInfo.nickName=@"路见不平";
    cellInfo.ID=@"4";
    [cellInfoArray addObject:cellInfo];
    [nickNameArray addObject:cellInfo.nickName];
    [IDArray addObject:cellInfo.ID];
    
    self.nickNameArray=nickNameArray;
    self.cellInfoArray=cellInfoArray;
    self.IDArray=IDArray;
}
- (CellInfo_t *)newCellInfo{
    CellInfo_t *info=[CellInfo_t new];
    return info;
}
- (void)initTableView{
    self.myTableViewW.delegate=self;
    self.myTableViewW.dataSource=self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.mySearchController.active){
        return self.searchList.count;
    }else{
        return self.cellInfoArray.count;
    }
}
- (CellInfo_t *)getCellInfo:(NSInteger)row{
    return self.cellInfoArray[row];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellInfo_t *cellInfo=[self getCellInfo:indexPath.row];
    static NSString *cellID=@"RRAddFriendCell";
    RRAddFriendCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:cellID owner:self options:nil]lastObject];
    }
    if(self.mySearchController.active){
        cell.mynickName=self.searchList[indexPath.row];
    }else{
        PAImageView *image=[[PAImageView alloc]initWithFrame:cell.myimage.bounds backgroundProgressColor:[UIColor lightGrayColor] progressColor:nil image:nil];
        [cell.myimage addSubview:image];
        [image setImageURL:cellInfo.image];
        
        cell.mynickName=cellInfo.nickName;
        cell.myID=cellInfo.ID;
    }
    [cell.myapplyAdd setTitle:@"申请添加" forState:UIControlStateNormal];
    [cell.myapplyAdd addTarget:self action:@selector(applyAdd) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSString *searchString = self.mySearchController.searchBar.text;
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];//用predicateWithFormat创建一个谓词,name作为键路径
    if (self.searchList!= nil) {
        [self.searchList removeAllObjects];
    }
    //过滤数据
    self.searchList= [NSMutableArray arrayWithArray:[self.nickNameArray filteredArrayUsingPredicate:preicate]];
    [self.myTableViewW reloadData];
}
- (void)applyAdd{
    NSLog(@"申请添加");
}
- (void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    [searchBar endEditing:YES];
    return YES;
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar endEditing:YES];
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。