UISearchDisplayController和UISearchBar一起使用用来管理UISearchBar和搜索结果的展示。
UISearchDisplayController提供了显示搜索结果的tableview来覆盖原控制器的视图;
使用UISearchDisplayController须要:
一般是在在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
展现或者隐藏搜索视图
内容来自苹果文档