IOS中搜索框UISearchBar及搜索方法的使用

搜索框可以用UISearchBar,并且可以设置代理UISearchBarDelegate。

 1 -(void)addSearchBar{
 2     CGRect searchBarRect = CGRectMake(0, 0, self.view.frame.size.width, 44);
 3     UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:searchBarRect];
 4     searchBar.placeholder = @"plrase enter key word";
 5     self.tableView.tableHeaderView = searchBar;
 6     //设置搜索框旁边的取消按钮
 7     searchBar.showsCancelButton = YES;
 8     //设置搜索范围
 9     searchBar.showsScopeBar = YES;
10     searchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"姓名",@"性别",@"学号", nil];
11     //代理
12     searchBar.delegate = self;
13 }

效果如图:

对结果搜索的处理也不是很麻烦,原理就是字符串中是否包含搜索的关键字,然后刷新tableView

//contact.firstName为所有数据,keyWord为搜索的关键字
if ([contact.firstName.uppercaseString containsString:keyWord.uppercaseString]) {
    [_searchContacts addObject:contact];
}

实现代理可以用

#pragma mark 搜索框代理
#pragma mark 取消搜索
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    //放弃第一响应者对象,关闭软键盘
    [self.searchBar resignFirstResponder];
}

#pragma mark 输入搜索关键字
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
}

#pragma mark 点击虚拟键盘上的搜索时
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.searchBar resignFirstResponder];
}

效果如下:

也可以用更简单的UISearchDisplayController,它内部有一个UITableView类型的对象searchResultsTableView,就可以不用上边的3个代理了。只需实现1个。UISearchDisplayController在使用UINavavigationController时还可以自动全屏,效果为

#pragma mark - UISearchDisplayController代理方法
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    [self searchDataWithKeyWord:searchString];
    return YES;
}

添加UISearchDisplayController时,有两种写法。需要注意,否则会报
Property 'searchDisplayController' attempting to use instance variable '_searchDisplayController' declared in super class 'UIViewController'
第一种,用property形式,需要@synthesize修饰才可以

@interface ContactsTableViewController ()<UISearchBarDelegate,UISearchDisplayDelegate>

@property(nonatomic,strong) UISearchBar *searchBar;
@property(nonatomic,strong) UISearchDisplayController *searchDisplayController;

@end

@implementation ContactsTableViewController
//需要用synthesize修饰
@synthesize searchDisplayController;

//此时的添加搜索栏可以用
-(void)addSearchBar{
    _searchBar=[[UISearchBar alloc]init];
    [_searchBar sizeToFit];//大小自适应容器
    _searchBar.placeholder = @"plrase enter key word";
    _searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _searchBar.showsCancelButton = YES;//显示取消按钮
    _searchBar.delegate=self;
    self.tableView.tableHeaderView=_searchBar;

    self.searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
    self.searchDisplayController.delegate = self;
    self.searchDisplayController.searchResultsDataSource = self;
    self.searchDisplayController.searchResultsDelegate = self;
    [self.searchDisplayController setActive:NO animated:YES];
}

@end

第二种,用带下划线的形式,无需@synthesize修饰

@interface ContactsTableViewController ()<UISearchBarDelegate,UISearchDisplayDelegate>{
    UISearchDisplayController *_searchDisplayController;
}

@property(nonatomic,strong) UISearchBar *searchBar;

@end

@implementation ContactsTableViewController

//此时添加搜索栏为
-(void)addSearchBar{
    self.searchBar = [[UISearchBar alloc]init];
    [self.searchBar sizeToFit];//大小自适应容器
    self.searchBar.placeholder = @"plrase enter key word";
    self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.searchBar.showsCancelButton = YES;//显示取消按钮
    self.searchBar.delegate=self;
    self.tableView.tableHeaderView = self.searchBar;
    
    _searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
    _searchDisplayController.delegate = self;
    _searchDisplayController.searchResultsDataSource = self;
    _searchDisplayController.searchResultsDelegate = self;
    [_searchDisplayController setActive:NO animated:YES];
}

@end

 

转载于:https://www.cnblogs.com/Apologize/p/4844775.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值