您的位置:首页 > 产品设计 > UI/UE

iOS开发 ----- UISearchBar_UISearchController

2015-09-22 20:03 393 查看

UISearchBar

简介

提供简单的搜索框,方便用户使用
一般来说配合UISearchController使用
由于iOS8之后不支持UISearchDisplayController,所以建议用上面的


相关属性

位置

_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];


样式

_searchBar.barStyle = UISearchBarIconSearch;


是否显示取消按钮

_searchBar.showsCancelButton = YES;


是否显示搜索结果按钮

_searchBar.showsSearchResultsButton = YES;


是否显示书签按钮

_searchBar.showsBookmarkButton = YES;


默认提示文字

_searchBar.placeholder = @"请输入文字";


光标的颜色

_searchBar.tintColor = [UIColor greenColor];


取消按钮后者其他按钮的颜色

_searchBar.barTintColor = [UIColor blueColor];


UISearchController

简介

自带一个UISearchBar,提供了一些列的方法,含有两个代理
想要实现搜索功能,必须遵守这个协议UISearchResultsUpdating


初始化,最后的那个self,nil都可以,放到tableView上的时候应该为no

_searchController = [[UISearchController alloc]initWithSearchResultsController:self];


设置代理

_searchController.searchResultsUpdater = self;


设置是否在搜索的时候,主界面半透明,要想搜索出来的数据可以点击的话,要为no

_searchController.dimsBackgroundDuringPresentation = YES;


设置是否隐藏导航栏,一般来说,搜索栏都会放到导航栏上,所以一般都是NO

_searchController.hidesNavigationBarDuringPresentation = NO;


大小位置

_searchController.searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);


放在导航栏的titleView上

self.navigationItem.titleView = _searchController.searchBar;


代理

必须实现这个方法
执行搜索会调用这个方法,在这个方法中,取到搜索框的字符串,然后在属于组中查找,被查找的数组里边又时一个数组,把相匹配的东西取出来,然后重载tableView
UISearchController有一个active属性,当点击搜索框的时候,active会变成YES,所以在tableView的一些列代理中要改变section和row


点击搜索框的时候调用的方法

-(void)updateSearchResultsForSearchController:(nonnull UISearchController *)searchController
{
if (_searchArr!=nil) {
[_searchArr removeAllObjects];
}

NSString * str = _searchController.searchBar.text;

for (NSArray * arr in _dataArr) {
//_searchArr = [NSMutableArray arrayWithArray:[arr filteredArrayUsingPredicate:prcdicate]];

for (NSString * string in arr) {
if ([string rangeOfString:str].location!=NSNotFound) {
[_searchArr addObject:string];
}
}
}

[_tableView reloadData];

}
//正则表达式搜索

NSString  * str = searchController.searchBar.text;

//SELF MATCHS %@        邮箱的正则
//SELf CONTAINS [c]%@   是否包含
NSPredicate * pr = [NSPredicate predicateWithFormat:@"SELf CONTAINS [c]%@",str];

_searchArray = [NSMutableArray arrayWithArray:[_dataArray filteredArrayUsingPredicate:pr]];


tableView有多少个section,当点击搜索框的时候,使section变为1个

-(NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView
{
if (_searchController.active) {
return 1;
}else
{
return _dataArr.count;
}

}


每个section有多少个row,由于搜索后之后一个section,所以,应该返回的是包含匹配到搜索框内容的数据的那个数组的个数,也就是代理方法中筛选出的那个数组

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

if (_searchController.active) {
return _searchArr.count;
}else
{
return [[_dataArr objectAtIndex:section] count];
}

}


在给cell填数据的时候也是这样,没激活时,是一个数组,激活时是一个数组

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

NSString * cellID = @"cellID";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID ];
}

if (_searchController.active) {
cell.textLabel.text = [_searchArr objectAtIndex:indexPath.row];
}else
{

cell.textLabel.text = [[_dataArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
return cell;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: