日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS - UISearchController

發布時間:2025/5/22 编程问答 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 = searchBar
    • UISearchDisplayDelegate 協議方法

      // 更新搜索結果/*只要搜索框的文字發生了改變,這個方法就會觸發。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.searchBar
    • UISearchResultsUpdating 協議方法

      // 更新搜索結果/*只要搜索框的文字發生了改變,這個方法就會觸發。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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩成人精品一区二区三区 | 欧美色图在线视频 | 欧美高清久久 | 91麻豆国产| 新香蕉视频 | 亚洲熟妇无码乱子av电影 | 精品国产欧美 | 免费看国产视频 | a级片毛片 | 日韩视频精品一区 | 亚洲AV无码国产精品播放在线 | 日韩和的一区二区 | 已婚少妇美妙人妻系列 | 精品人妻午夜一区二区三区四区 | 疯狂做受xxxx国产 | 久久久久久久综合 | 欧美天天搞 | 在线观看视频亚洲 | 国产调教av| 国产精品黑丝 | 美脚の诱脚舐め脚 | www.brazzers.com| 国产一区二区三区网站 | 中文字幕理伦片免费看 | 少妇高潮一区二区三区99 | 日韩不卡一区二区三区 | 日韩免费一区二区三区 | 国产av一区二区三区传媒 | 国产成人av免费看 | 日本大尺度电影免费观看全集中文版 | 欧美一级片在线免费观看 | 国产精品国产三级国产专区52 | 音影先锋av资源 | 日韩在线观看免费 | 宅男噜噜噜666在线观看 | www.国产.com | 午夜精品一区二 | 国产一级自拍 | 在线成人免费视频 | 999久久久免费精品国产 | 操操操综合网 | 欧美成人精品二区三区99精品 | 性做久久久久久久久久 | 91免费网站在线观看 | 亚洲精品成人片在线观看精品字幕 | 91视频免费看| 国产一级黄色 | www.五月婷婷| 久久久水蜜桃 | 亚洲在线观看一区 | 99成人 | 欧美中文字幕第一页 | 蜜桃av成人 | 蜜臀av在线免费观看 | 九色论坛 | 国产精品自拍一区 | 伊人久久久久噜噜噜亚洲熟女综合 | 天天综合亚洲 | 国产一级免费 | 成人国产精品免费观看视频 | 国产女人18毛片水18精品 | 欧美日韩免费在线观看 | 激情av中文字幕 | 国产精品无码AV无码国产 | 国产精品毛片久久久 | 97超碰97 | 午夜一区在线观看 | 国产成人啪精品午夜在线观看 | 福利色导航 | 在线免费h| 亚洲精品一区二区三区新线路 | 日韩精品无码一区二区三区 | 欧美三级黄色大片 | 99精品视频在线观看 | 亚洲视频黄 | а天堂中文在线官网 | jizz中文字幕 | 青青草97国产精品麻豆 | 美日韩成人 | 中文字幕精品一区二区三区精品 | 女人扒开腿让男人桶爽 | 欧美在线一级片 | 日日夜夜狠狠干 | 97精品一区 | 91视频专区 | 亚洲综合av一区二区三区 | 福利视频三区 | 一区二区三区视频观看 | 日韩xxx高潮hd | 苍井空亚洲精品aa片在线播放 | 欧美日韩精品一区二区在线播放 | 黄网站在线观看 | 日本综合色 | 亚洲女同一区 | av网址有哪些 | 伊人久久综合 | 日本欧美久久久 | 成人性生交大片免费看r链接 | 中国女人内精69xxxxxx |