搜索内容
<UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating>
{
//创建数组
NSArray *arr;
NSArray *arr1;
//表格
UITableView *tab;
UISearchController *se1;
}
[super viewDidLoad];
self.navigationItem.title=@"我的消息";
//创建对象并初始化
Modi *a1=[[Modi alloc]init];
a1.name=@"张三";
a1.xiao=@"二货";
a1.tu=@"1111";
a1.time = @"29:10";
Modi *a2=[[Modi alloc]init];
a2.name=@"李四";
a2.xiao=@"二货";
a2.tu=@"1111";
a2.time = @"29:10";
Modi *a3=[[Modi alloc]init];
a3.name=@"张三";
a3.xiao=@"二货";
a3.tu=@"1111";
a3.time = @"29:10";
Modi *a4=[[Modi alloc]init];
a4.name=@"张三";
a4.xiao=@"二货";
a4.tu=@"1111";
a4.time = @"29:10";
arr=@[a1,a2,a3,a4];
//设置表格
tab=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//设置代理
tab.dataSource=self;
tab.delegate=self;
[self.view addSubview:tab];
se1=[[UISearchController alloc]initWithSearchResultsController:nil];
se1.searchResultsUpdater=self;
se1.searchBar.backgroundColor=[UIColor redColor];
//将搜索控制器搜索设置表格头
tab.tableHeaderView=se1.searchBar;
}
//设置关联
- (void)updateSearchResultsForSearchController:(UISearchController *)searchControll
{
//创建谓词,查找“小”的对象
NSPredicate *pre=[NSPredicate predicateWithFormat:@"name contains %@",searchControll.searchBar.text];
//执行过滤
arr1=[arr filteredArrayUsingPredicate:pre];
//刷新表格
[tab reloadData];
}
//设置分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//设置内容行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (se1.isActive == YES)
{
return arr1.count;
}
else
{
return arr.count;
}
return 0;
}
//设置表格内容
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *str=@"cell";
UITableViewCell *cel=[tableView dequeueReusableCellWithIdentifier:str];
if (cel==nil)
{
cel=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}
//赋值
if (se1.isActive == YES)
{
Modi *m1 = arr[indexPath.row];
cel.textLabel.text=m1.name;
cel.imageView.image=[UIImage imageNamed:m1.tu];
cel.detailTextLabel.text=m1.xiao;
}
else
{
Modi *m2=arr[indexPath.row];
cel.textLabel.text=m2.name;
cel.detailTextLabel.text=m2.xiao;
cel.imageView.image=[UIImage imageNamed:m2.tu];
}
return cel;
}