標籤:ios search 搜尋
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
展現或者隱藏搜尋視圖
內容來自蘋果文檔
iOS UISearchDisplayController學習筆記