iOS UISearchDisplayController学习笔记

UISearchDisplayController和UISearchBar一起使用用来管理UISearchBar和搜索结果的展示。UISearchDisplayController提供了显示搜索结果的tableview来覆盖原控制器的视图;

使用UISearchDisplayController需要:

  • 提供搜索结果table的数据的来源-searchResultsDataSource
  • 搜索结果table的代理 SearchResultsDelegate
  • UISearchDisplayController控制器的代理delegate ,相应搜索事件的开始结束和显示隐藏界面;(这个代理知道搜索字符串的改变和搜索范围,所以结果table能够(自动)重新导入)
  • searchBar的代理(关于UISearchBar代理上篇文章已经说明)

通常是在在uitableview中初始化UISearchDisplayController来展示一个列表;

searchController = [[UISearchDisplayController alloc]
                         initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;

在tabelview代理方法中需要判断是哪一个table(UITableViewController中有self.tableView  UISearchDisplayController中有.searchResultsTableView)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
    if (tableView == self.tableView) {
        return ...;
    }
    // If necessary (if self is the data source for other table views),
    // check whether tableView is searchController.searchResultsTableView.
    return ...;
}
一个UIViewController和一个UISearchBar只有一个UISearchDisplayController;


你能够在ios7 之后,在navigationbar中使用SearchDisplayController,通过配置UISearchDisplayController中得displaysSearchBarInNavigationBar和 navigationItem属性;


相关的属性和方法

@property(nonatomic, getter=isActive) BOOL active

展现(是否隐藏)状态,默认值是NO,直接设置没有动画,用setActive:animated: 设置会有动画

@property(nonatomic, assign) id<UISearchDisplayDelegate> delegate

代理

@property(nonatomic, assign) BOOL displaysSearchBarInNavigationBar

指定navigationbar中包含一个searchBar

@property(nonatomic, readonly) UINavigationItem *navigationItem

只读属性 代表在navigation controller的navigationbar中的searchdisplaycontroller;

@property(nonatomic, readonly) UISearchBar *searchBar

UISearchDisplayController中的UISearchBar;

@property(nonatomic, readonly) UIViewController *searchContentsController

这个属性管理着搜索出的内容

@property(nonatomic, assign) id<UITableViewDataSource> searchResultsDataSource

展示搜索结果的数据来源

@property(nonatomic, assign) id<UITableViewDelegate> searchResultsDelegate

搜索结果展示的table的代理

@property(nonatomic, readonly) UITableView *searchResultsTableView

搜索结果展示的table

@property(nonatomic, copy) NSString *searchResultsTitle

搜索结果视图的标题

- (id)initWithSearchBar:(UISearchBar *)searchBar contentsController:(UIViewController *)viewController

初始化控制器 指定关联的search 和view controller

- (void)setActive:(BOOL)visible animated:(BOOL)animated

展现或者隐藏搜索视图

内容来自苹果文档


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值