iOS - UISearchController
前言
NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearchController")@interface UISearchDisplayController : NSObject@available(iOS, introduced=3.0, deprecated=8.0, message="UISearchDisplayController has been replaced with UISearchController")public class UISearchDisplayController : NSObjectNS_CLASS_AVAILABLE_IOS(8_0) @interface UISearchController : UIViewController <UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning>@available(iOS 8.0, *) public class UISearchController : UIViewController, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning在 iOS 8.0 以上版本中, 我們可以使用 UISearchController 來非常方便地在 UITableView 中添加搜索框. 而在之前版本中, 我們還是必須使用 UISearchDisplayController + UISearchBar 的組合方式.
我們創建的 tableView 和搜索控制器創建的 tableView 都會走代理方法,需要在代理方法中判斷響應代理方法的 tableView 是哪一個,如果響應代理方法的 tableView 不是我創建的,說明一定是搜索控制器創建的。在 iOS 8.0 以下版本中需使用 tableView == myTableView 判斷,在 iOS 8.0 以上版本中需使用 mySearchController.active 判斷。
1、搜索框的創建
1.1 在 iOS 8.0 以下版本中
Objective-C
遵守協議 UISearchDisplayDelegate
搜索結果數組初始化
// 聲明搜索結果存放數組@property(nonatomic, retain)NSMutableArray *mySearchResultArray;// 初始化搜索結果存放數組mySearchResultArray = [[NSMutableArray alloc] init];searchDisplayController 初始化
// 聲明搜索控制器,自帶一個表格視圖,用來展示搜索結果,必須設置為全局變量@property(nonatomic, retain)UISearchDisplayController *mySearchDisplayController;// 實例化搜索條UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];// 實例化搜索控制器對象mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; // 設置搜索控制器的代理mySearchDisplayController.delegate = self;// 為搜索控制器自帶 tableView 指定代理mySearchDisplayController.searchResultsDelegate = self;mySearchDisplayController.searchResultsDataSource = self;// 將搜索條設置為 tableView 的表頭myTableView.tableHeaderView = searchBar;UISearchDisplayDelegate 協議方法
// 更新搜索結果/*只要搜索框的文字發生了改變,這個方法就會觸發。searchString 為搜索框內輸入的內容。*/- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {// 清空上一次搜索的內容[mySearchResultArray removeAllObjects];// 將搜索的結果存放到數組中for (NSArray *subArray in myDataArray) {for (NSString *str in subArray) {NSRange range = [str rangeOfString:searchString];if (range.length) {[mySearchResultArray addObject:str];}}}return YES;}UITableView 協議方法
// 設置分段頭標題- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {if (tableView == myTableView) {return [NSString stringWithFormat:@"%c", (char)('A' + section)];}return @"搜索結果";}// 設置分段數- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {if (tableView == myTableView) {return myDataArray.count;}return 1;}// 設置行數- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {if (tableView == myTableView) {return [[myDataArray objectAtIndex:section] count];}return mySearchResultArray.count;}// 設置每段顯示的內容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testIdentifier"];if (!cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testIdentifier"];}if (tableView == myTableView) {cell.textLabel.text = [[myDataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];}else {cell.textLabel.text = [mySearchResultArray objectAtIndex:indexPath.row];}return cell;}
Swift
遵守協議 UISearchDisplayDelegate
搜索結果數組初始化
// 初始化搜索結果存放數組var mySearchResultArray:[String] = Array()searchDisplayController 初始化
// 聲明搜索控制器,自帶一個表格視圖,用來展示搜索結果,必須設置為全局變量var mySearchDisplayController:UISearchDisplayController!// 實例化搜索條let searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))// 實例化搜索控制器對象mySearchDisplayController = UISearchDisplayController(searchBar: searchBar, contentsController: self)// 設置搜索控制器的代理mySearchDisplayController.delegate = self// 為搜索控制器自帶 tableView 指定代理mySearchDisplayController.searchResultsDelegate = selfmySearchDisplayController.searchResultsDataSource = self// 將搜索條設置為 tableView 的表頭myTableView.tableHeaderView = searchBarUISearchDisplayDelegate 協議方法
// 更新搜索結果/*只要搜索框的文字發生了改變,這個方法就會觸發。searchString 為搜索框內輸入的內容*/func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String?) -> Bool {// 清空上一次搜索的內容mySearchResultArray.removeAll()// 將搜索的結果存放到數組中for subArray in myDataArray {for str in subArray {let range:NSRange = (str as NSString).rangeOfString(searchString!)if range.length != 0 {mySearchResultArray.append(str)}}}return true}UITableView 協議方法
// 設置分段頭標題func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {if tableView == myTableView {return "\(Character(UnicodeScalar(65 + section)))"}return "搜索結果"}// 設置分段數func numberOfSectionsInTableView(tableView: UITableView) -> Int {if tableView == myTableView {return myDataArray.count}return 1}// 設置行數func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {if tableView == myTableView {return myDataArray[section].count}return mySearchResultArray.count}// 設置每段顯示的內容func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {var cell = tableView.dequeueReusableCellWithIdentifier("testIdentifier")if cell == nil {cell = UITableViewCell(style: .Default, reuseIdentifier: "testIdentifier")}if tableView == myTableView {cell!.textLabel?.text = myDataArray[indexPath.section][indexPath.row]}else {cell!.textLabel?.text = mySearchResultArray[indexPath.row]}return cell!}
1.2 在 iOS 8.0 及以上版本中
Objective-C
遵守協議 UISearchResultsUpdating
搜索結果數組初始化
// 聲明搜索結果存放數組@property(nonatomic, retain)NSMutableArray *mySearchResultArray;// 初始化搜索結果存放數組mySearchResultArray = [[NSMutableArray alloc] init];searchController 初始化
// 聲明搜索控制器,自帶一個表格視圖控制器,用來展示搜索結果,必須設置為全局變量@property(nonatomic, retain)UISearchController *mySearchController;// 實例化搜索控制器mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];// 設置搜索代理mySearchController.searchResultsUpdater = self;// 設置搜索條大小[mySearchController.searchBar sizeToFit];// 設置搜索期間背景視圖是否取消操作,default is YESmySearchController.dimsBackgroundDuringPresentation = NO;// 設置搜索期間是否隱藏導航條,default is YESmySearchController.hidesNavigationBarDuringPresentation = NO;// 將 searchBar 添加到表格的開頭myTableView.tableHeaderView = mySearchController.searchBar;UISearchResultsUpdating 協議方法
// 更新搜索結果/*只要搜索框的文字發生了改變,這個方法就會觸發。searchController.searchBar.text 為搜索框內輸入的內容*/- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {// 清除上一次的搜索結果[mySearchResultArray removeAllObjects];// 將搜索的結果存放到數組中for (NSArray *subArray in myDataArray) {for (NSString *str in subArray) {NSRange range = [str rangeOfString:searchController.searchBar.text];if (range.length) {[mySearchResultArray addObject:str];}}}// 重新加載表格視圖,不加載的話將不會顯示搜索結果[myTableView reloadData];}UITableView 協議方法
// 設置分段頭標題- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {if (mySearchController.active) {return @"搜索結果";}return [NSString stringWithFormat:@"%c", (char)('A' + section)];}// 設置分段數- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {if (mySearchController.active) {return 1;}return myDataArray.count;}// 設置行數- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {if (mySearchController.active) {return mySearchResultArray.count;}return [[myDataArray objectAtIndex:section] count];}// 設置每段顯示的內容- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testIdentifier"];if (!cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testIdentifier"];}if (mySearchController.active) {cell.textLabel.text = [mySearchResultArray objectAtIndex:indexPath.row];}else {cell.textLabel.text = [[myDataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];}return cell;}
Swift
遵守協議 UISearchResultsUpdating
搜索結果數組初始化
// 初始化搜索結果存放數組var searchResultArray:[String] = Array()searchController 初始化
// 聲明搜索控制器,自帶一個表格視圖控制器,用來展示搜索結果,必須設置為全局變量var mySearchController:UISearchController!// 實例化搜索控制器mySearchController = UISearchController(searchResultsController: nil)// 設置搜索代理mySearchController.searchResultsUpdater = self// 設置搜索條大小mySearchController.searchBar.sizeToFit()// 設置搜索期間背景視圖是否取消操作,default is YESmySearchController.dimsBackgroundDuringPresentation = false// 設置搜索期間是否隱藏導航條,default is YESmySearchController.hidesNavigationBarDuringPresentation = false// 將 searchBar 添加到表格的開頭myTableView.tableHeaderView = mySearchController.searchBarUISearchResultsUpdating 協議方法
// 更新搜索結果/*只要搜索框的文字發生了改變,這個方法就會觸發。searchController.searchBar.text 為搜索框內輸入的內容*/func updateSearchResultsForSearchController(searchController: UISearchController) {// 清除上一次的搜索結果searchResultArray.removeAll()// 將搜索的結果存放到數組中 for subArray in myDataArray {for str in subArray {let range:NSRange = (str as NSString).rangeOfString(searchController.searchBar.text!)if range.length != 0 {searchResultArray.append(str)}}}// 重新加載表格視圖,不加載的話將不會顯示搜索結果myTableView.reloadData()}UITableView 協議方法
// 設置分段頭標題func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {if mySearchController.active {return "搜索結果"}return "\(Character(UnicodeScalar(65 + section)))"}// 設置分段數func numberOfSectionsInTableView(tableView: UITableView) -> Int {if mySearchController.active {return 1}return myDataArray.count}// 設置行數func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {if mySearchController.active {return searchResultArray.count}return myDataArray[section].count}// 設置每段顯示的內容func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("testIdentifier")if cell == nil {cell = UITableViewCell(style: .Default, reuseIdentifier: "testIdentifier")}if mySearchController.active {cell!.textLabel?.text = searchResultArray[indexPath.row]}else {cell!.textLabel?.text = myDataArray[indexPath.section][indexPath.row]}return cell!}
總結
以上是生活随笔為你收集整理的iOS - UISearchController的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu关闭ipv6
- 下一篇: Atitit.去除水印的方案