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

展现或者隐藏搜索视图

内容来自苹果文档


原文地址:https://www.cnblogs.com/gccbuaa/p/6727242.html