日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 狠狠久久久 | 麻豆免费在线观看视频 | 国产专区精品 | 超碰免费在线播放 | 泷泽萝拉在线播放 | 国产免费自拍视频 | 在线亚洲人成电影网站色www | 丰满少妇在线观看资源站 | 美国特色黄a大片 | 免费成人av在线 | 天堂av免费在线 | 日韩a级在线观看 | 欧美一区二区在线看 | 欧美日韩国产一区二区在线观看 | 亚洲中文字幕无码专区 | 日韩在线中文字幕 | 天天想你在线观看完整版电影免费 | 日韩精品久久久久久 | 国产91亚洲精品 | 国产看片网站 | 国产精品久久久久野外 | 九一亚洲精品 | 亚洲成人第一 | 人体内射精一区二区三区 | 午夜tv影院 | 亚洲我射av | 肉色超薄丝袜脚交一区二区 | 男人日女人网站 | 日本在线播放 | 天堂中文在线8 | 蜜桃av成人 | 草草在线观看 | 欧美日韩国产一区二区三区在线观看 | 性中文字幕 | 麻豆天天躁天天揉揉av | 丰满熟女一区二区三区 | 国产成人在线视频免费观看 | 国产精品无码久久久久久电影 | 日韩欧美国产一区二区三区在线观看 | 国产精品无码久久久久久电影 | 三级免费黄录像 | 国产av剧情一区二区三区 | 女同爱爱视频 | 奇米影视在线播放 | 国产成人综合在线视频 | 可以看毛片的网站 | 国产原创中文av | 久久久久久久九九九九 | 亚洲欧美日韩图片 | 星空大象mv高清在线观看免费 | 日本在线播放视频 | 毛片一级免费 | 天天操夜夜摸 | 高h喷汁呻吟3p | 国产69熟| 亚洲天堂最新 | 波多野结衣1区2区3区 | 国产东北露脸精品视频 | 农村妇女精品一区二区 | 久久这里只有精品8 | 欧美日韩高清丝袜 | 国产91精| 日韩视频一二三区 | 精品人妻一区二区三区久久夜夜嗨 | 台湾少妇xxxx做受 | 亚洲第一页色 | 黄色av成人 | 亚洲性在线 | 奇米在线视频 | 高清一区二区三区四区 | 91重口味| 在线观看国产一级片 | 欧美性色黄大片手机版 | 中文字幕精品久久久久人妻红杏ⅰ | 欧美第一页 | 在线五月天| 操啊操| 午夜欧美福利 | 女人18片毛片60分钟 | 日本一区二区三区视频在线播放 | 欧美日韩精品三区 | 亚洲香蕉网站 | 免费看的av | 久久久久久久影院 | 黄色香蕉网 | www日韩在线 | 一区二区三区久久 | av先锋资源 | 九九热视频在线观看 | 亚洲欧美日韩国产成人精品影院 | 伊人蕉久影院 | 婷婷亚洲一区 | 肌肉猛男裸体gay网站免费 | 伊人伊人 | 色女人影院 | 日本高清免费aaaaa大片视频 | 亚洲第一国产 | 91av视频在线免费观看 | 岛国av大片 |