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
时间: 2024-08-06 07:54:16

IOS中搜索框UISearchBar及搜索方法的使用的相关文章

ios中,在SearchBar里面搜索内容,可根据内容来查找所需的信息资源,可获得SearchBar中的内容

贴一段我很久以前写的小demo,你们就明白了,是把textField套在alertView里的@interface ViewController : UIViewController <UIAlertViewDelegate, UITextFieldDelegate>{ UILabel *la; UITextField *myTextField;} @implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; l

关于iOS中提示框的使用

关于iOS中提示框的使用在iOS8的SDK里,已经对提示框进行了更改,现在属于 UIAlertController,其接口如下声明NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController,不再是一个UIView. 具体的使用如下,希望对某些朋友有帮助.#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 //判断你当前的iOS SDK所支持的最大iOS系

在iOS中,实现点击搜索结果隐藏搜索结果的方法。

不知道有没有别的什么的好的方法,最近在实现一个需求(点击搜索,然后输入搜索内容,显示搜索出来的结果,然后点击搜索结果,在当前页面显示所点击的结果的详细的信息).遇到的问题是,点击搜索结果的时候,搜索的结果不隐藏,那么你就看不到被它挡着的东西了. 解决办法是 for (id view in subViews) { if ([view isKindOfClass:[UIButton class]]) { UIButton *cancelButton = (UIButton *)view; [canc

js搜索框实现自动搜索功能

做项目的时候,老板让我自己封装一个搜索功能,就类似于百度这种 输入了字符之后,就可以自动搜索数据,而且还会出现一个下拉框供用户选择,我觉得我老板有问题,网上有这么多插件,不仅封装好了,性能也做了优化,什么功能都有,他不用,一定要我用原生js写,写毛线写,我内心吐槽了很久,不过还是要做,为了工资而低头,所以我这个小白就硬着头皮写完了,肯定有很多不足,也没有封装,就是想到哪里写到哪里,给大家当反面教材看看,如果大家看见了,也可以指点指点我,让我进步 由于我是在项目里写的,所以有很多东西和大家的肯定不

iOS搜索框UISearchBar

当你在seachBar中输入字母之前的时候,只是用鼠标选中searchBar的时候,如图 终端输出截图如下:(这个时候调用先shouldBeginEditing,之后调用didBeginEditing,) 当你希望选中UISearchBar的时候,键盘自动调用加载到界面,你需要将下面函数的返回值设置为YES: - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { NSLog(@"shouldBeginEditing"

搜索框UISearchBar

1\初始化 UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10, 20, 200, 30)]; [self.view addSubview:searchBar]; searchBar.delegate = self; 2\实现代理方法 -(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { NSLog(@"开始编辑"); [

IOS中Json解析的四种方法

作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验(点击打开链接).此网站不仅可以检测Json代码中的错误,而且可以以视图形式显示json中的数据内容,很是方便. 从IOS5开始,APPLE提供了对json的原生支持(NSJSONSerialization),但是为了兼容以前的ios版本,可以使用第三方库来解析Json. 本文将介绍TouchJso

【转】IOS中Json解析的四种方法

原文网址:http://blog.csdn.net/enuola/article/details/7903632 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验(点击打开链接).此网站不仅可以检测Json代码中的错误,而且可以以视图形式显示json中的数据内容,很是方便. 从IOS5开始,APPLE提供了对json的原生支持(NSJSONSer

iOS中创建动态库及调用方法

去年因需要用到动态库,自己就找了好多一些 资料,最终找到了一套方法,怎么创建与使用动态库,记录一下: Xcode提供了在iOS工程中创建静态库的功能,和在MAC上创建动态库和静态库的功能. 但是没有提供在iOS工程中创建动态库的功能(苹果官方不允许程序中存在动态库链接,这样的程序会被AppStore拒),如下图:  由于苹果不支持自己创建iOS动态库,所以要想创建动态库首先要修改Xcode的配置文件使其支持具备创建iOS 动态库的功能, 经过调研和查询网上的一些资料,并经过自己测试成功,以下是修