searchcontroller使用

#import"TableViewController.h"

@interfaceTableViewController()

@property(nonatomic,strong)UISearchController*searchController;

@property(nonatomic,strong)NSMutableArray*listArr;

@property(nonatomic,strong)NSMutableArray*dataArr;

@end

@implementationTableViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.listArr=[NSMutableArrayarray];

self.dataArr=[NSMutableArrayarray];

for(inti=0; i<100; i++) {

NSString*s=[NSStringstringWithFormat:@"%d",i];

[self.dataArraddObject:s];

}

self.searchController=[[UISearchControlleralloc]initWithSearchResultsController:nil];

_searchController.searchResultsUpdater=self;

_searchController.dimsBackgroundDuringPresentation=NO;

_searchController.hidesNavigationBarDuringPresentation=YES;

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

self.tableView.tableHeaderView=self.searchController.searchBar;

[self.tableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:@"cell"];

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {

// Return the number of sections.

return1;

}

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

if(self.searchController.active) {

return[self.listArrcount];

}else{

return[self.dataArrcount];

}

}

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

UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];

if(self.searchController.active) {

cell.textLabel.text=self.listArr[indexPath.row];

}else{

cell.textLabel.text=self.dataArr[indexPath.row];

}

returncell;

}

-(void)updateSearchResultsForSearchController:(UISearchController*)searchController {

NSString*searchString = [self.searchController.searchBartext];

NSPredicate*preicate = [NSPredicatepredicateWithFormat:@"SELF CONTAINS

%@", searchString];

if(self.listArr!=nil) {

[self.listArrremoveAllObjects];

}

//过滤数据

self.listArr= [NSMutableArrayarrayWithArray:[self.dataArrfilteredArrayUsingPredicate:preicate]];

//刷新表格

[self.tableViewreloadData];

}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

NSLog(@"%ld",indexPath.row);

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,301评论 3 38
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,588评论 9 5
  • 一个普通的searchBar的使用 写在前面 这两天想给app加一个搜索功能,打算采用系统自带的UISearch等...
    早起的虫儿子被鸟吃阅读 1,831评论 0 2
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 1,195评论 0 0
  • Finally I got the 3D design software done... I have worke...
    欧阳去非阅读 201评论 0 0

友情链接更多精彩内容