iOS UISearchController的使用

來源:互聯網
上載者:User

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];
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.