iOS UISearchDisplayController學習筆記

來源:互聯網
上載者:User

標籤: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學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.