搜索框使用,UISearchBarDelegate
2016-07-16 10:53
537 查看
1.首先
@interface RootViewController :UIViewController <UISearchBarDelegate>
2.创建 它,设置代理
_searchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(0,0,320,30)];
_searchBar.delegate =self;
_tableView.tableHeaderView =_searchBar;
//UISearchDisplayController与搜索框关联后当在搜索框中输入内容时,会弹出一个UISearchDisplayController自己创建的TableView把我们原来的TableView覆盖。UISearchDisplayController的searchResultsDataSource
searchResultsDelegate分别是其创建的那个Tableview的dataSource和delegate属性
//这里我们把自己创建的TableView(_tableView)和UISearchDisplayController创建的TableView的代理都设成了self,所以在相应的事件处理函数里需要判断触发事件的到底是哪个TableView.
UISearchDisplayController *searchDisplayController = [[UISearchDisplayControlleralloc]initWithSearchBar:_searchBarcontentsController:self];
searchDisplayController.searchResultsDataSource =self;
searchDisplayController.searchResultsDelegate =self;
3.代理方法
//定制搜索框的取消按钮,纯粹是摸索出来的仅供参考。
- (void)customSearchBar
{
for (UIView *viewin [_searchBar.subviews[0]subviews])
{
if ([viewisKindOfClass:UIButton.class])
{
UIButton *button = (UIButton *)view;
[button
setTitle:@"取消"forState:UIControlStateNormal];
}
}
}
#pragma mark-UISearchBarDelegate
//UISearchBarDelegate协议中定义的方法,当开始编辑时(搜索框成为第一响应者时)被调用。
- (void)searchBarTextDidBeginEditing:(UISearchBar
*)searchBar
{
searchBar.showsCancelButton =YES;
[self customSearchBar];
}
-(void)searchBarCancelButtonClicked:(UISearchBar
*)searchBar{
[_tableViewreloadData];
}
@interface RootViewController :UIViewController <UISearchBarDelegate>
2.创建 它,设置代理
_searchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(0,0,320,30)];
_searchBar.delegate =self;
_tableView.tableHeaderView =_searchBar;
//UISearchDisplayController与搜索框关联后当在搜索框中输入内容时,会弹出一个UISearchDisplayController自己创建的TableView把我们原来的TableView覆盖。UISearchDisplayController的searchResultsDataSource
searchResultsDelegate分别是其创建的那个Tableview的dataSource和delegate属性
//这里我们把自己创建的TableView(_tableView)和UISearchDisplayController创建的TableView的代理都设成了self,所以在相应的事件处理函数里需要判断触发事件的到底是哪个TableView.
UISearchDisplayController *searchDisplayController = [[UISearchDisplayControlleralloc]initWithSearchBar:_searchBarcontentsController:self];
searchDisplayController.searchResultsDataSource =self;
searchDisplayController.searchResultsDelegate =self;
3.代理方法
//定制搜索框的取消按钮,纯粹是摸索出来的仅供参考。
- (void)customSearchBar
{
for (UIView *viewin [_searchBar.subviews[0]subviews])
{
if ([viewisKindOfClass:UIButton.class])
{
UIButton *button = (UIButton *)view;
[button
setTitle:@"取消"forState:UIControlStateNormal];
}
}
}
#pragma mark-UISearchBarDelegate
//UISearchBarDelegate协议中定义的方法,当开始编辑时(搜索框成为第一响应者时)被调用。
- (void)searchBarTextDidBeginEditing:(UISearchBar
*)searchBar
{
searchBar.showsCancelButton =YES;
[self customSearchBar];
}
-(void)searchBarCancelButtonClicked:(UISearchBar
*)searchBar{
[_tableViewreloadData];
}
相关文章推荐
- easyUI中文乱码
- 96. Unique Binary Search Trees
- 我所了解的MessageQueue的消息存放过程
- 滑动视图的循环滑动
- UVA-10420 List of Conquests
- iOS开发从入门到精通--UIWindow基础
- 357. Count Numbers with Unique Digits
- java序列号 SerializeUID
- aapt.exe finished with nonzero exit value 1
- 关于面试中的一二 ------------UI控件 持续更新
- AbstractQueue抽象类源码解析
- Deque接口源码解析
- 解决“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误
- codeforces 360 E - The Values You Can Make
- Buildroot-2012.08交叉编译器制作
- Queue接口源码解析
- android studio中requestWindowFeature(Window.FEATURE_NO_TITLE)隐藏标题栏失效的解决方案
- iOS开发之UITextField 左侧设置小图标 leftview
- 62. Unique Paths
- MySQL提示:The server quit without updating PID file(…)失败