麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

IOS之表視圖添加搜索欄

2019-11-14 19:41:38
字體:
來源:轉載
供稿:網友

       下面是我們要實現的效果。本效果是在上一篇自定義表視圖的基礎上進行更改的。

   

1.將Search bar and search display拖動到ViewController中。不要添加Search Bar.

 

2.修改ViewController的頭文件

Cpp代碼 復制代碼 收藏代碼
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface IkrboyViewController4 : UIViewController  
  4. {  
  5.     NSArray *dataArr;//用于顯示表視圖的數據  
  6.     NSArray *allDataArr;//存儲所有數據,通過關鍵詞搜索將搜索結果賦值給dataArr  
  7. }  
  8. @PRoperty (weak, nonatomic) IBOutlet UISearchBar *searchBar;  
  9.   
  10. @end  
#import <UIKit/UIKit.h>@interface IkrboyViewController4 : UIViewController{    NSArray *dataArr;//用于顯示表視圖的數據    NSArray *allDataArr;//存儲所有數據,通過關鍵詞搜索將搜索結果賦值給dataArr}@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;@end

 

 3.修改自定義方法initTableViewData。將ScopeBar隱藏是考慮到iphone的顯示高度問題。可自行決定。

Cpp代碼 復制代碼 收藏代碼
  1. -(void)initTableViewData{  
  2.     NSBundle *bundle = [NSBundle mainBundle];  
  3.     NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];  
  4.     allDataArr = [[NSArray alloc] initWithContentsOfFile:plistPath];  
  5.     dataArr = [NSArray arrayWithArray:allDataArr];  
  6.     NSLog(@"table data count = %d",[allDataArr count]);  
  7.       
  8.     //設定搜索欄ScopeBar隱藏  
  9.     [self.searchBar setShowsScopeBar:NO];  
  10.     [self.searchBar sizeToFit];  
  11. }  
-(void)initTableViewData{    NSBundle *bundle = [NSBundle mainBundle];    NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];    allDataArr = [[NSArray alloc] initWithContentsOfFile:plistPath];    dataArr = [NSArray arrayWithArray:allDataArr];    NSLog(@"table data count = %d",[allDataArr count]);        //設定搜索欄ScopeBar隱藏    [self.searchBar setShowsScopeBar:NO];    [self.searchBar sizeToFit];}

4.添加SearchBar的三個事件觸發

Cpp代碼 復制代碼 收藏代碼
  1. //以下三個方法實現SearchBar的搜索功能  
  2. //當文本內容發生改變時候,向表視圖數據源發出重新加載消息  
  3. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString  
  4. {  
  5.     [self filterContentForSearchText:searchString scope:self.searchBar.selectedScopeButtonIndex];  
  6.     //YES情況下表視圖可以重新加載  
  7.     return YES;  
  8. }  
  9.   
  10. // 當Scope Bar選擇發送變化時候,向表視圖數據源發出重新加載消息  
  11. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption  
  12. {  
  13.     [self filterContentForSearchText:self.searchBar.text scope:searchOption];  
  14.     // YES情況下表視圖可以重新加載  
  15.     return YES;  
  16. }  
  17.   
  18. //點擊cancel按鈕的事件  
  19. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar  
  20. {  
  21.     //查詢所有  
  22.     [self filterContentForSearchText:@"" scope:-1];  
  23. }  
//以下三個方法實現SearchBar的搜索功能//當文本內容發生改變時候,向表視圖數據源發出重新加載消息- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{    [self filterContentForSearchText:searchString scope:self.searchBar.selectedScopeButtonIndex];    //YES情況下表視圖可以重新加載    return YES;}// 當Scope Bar選擇發送變化時候,向表視圖數據源發出重新加載消息- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{    [self filterContentForSearchText:self.searchBar.text scope:searchOption];    // YES情況下表視圖可以重新加載    return YES;}//點擊cancel按鈕的事件- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{    //查詢所有    [self filterContentForSearchText:@"" scope:-1];}

   5.自定義關鍵詞搜索功能

Cpp代碼 復制代碼 收藏代碼
  1. //自定義搜索方法,根據關鍵詞從allDataArr中搜索到滿足搜索條件的元素,并將匹配的數組賦值給dataArr,由于dataArr是表視圖的數據源,因此表視圖的記錄也會隨之改變。  
  2. - (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope;  
  3. {  
  4.     if([searchText length]==0)  
  5.     {  
  6.         //查詢所有  
  7.         dataArr = [NSArray arrayWithArray:allDataArr];  
  8.         NSLog(@"dataArr count = %d",[dataArr count]);  
  9.         return;  
  10.     }  
  11.       
  12.     NSPredicate *scopePredicate;  
  13.       
  14.     switch (scope) {  
  15.         case 0:  
  16.             scopePredicate = [NSPredicate predicateWithFormat:@"(SELF.itemName contains[c] %@) OR (SELF.itemImagePath contains[c] %@)",searchText,searchText];  
  17.             NSLog(@"searchText=%@",searchText);  
  18.             dataArr =[NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]];  
  19.             break;  
  20.         case 1:  
  21.             scopePredicate = [NSPredicate predicateWithFormat:@"SELF.itemName contains[c] %@",searchText];  
  22.             dataArr = [NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]];  
  23.             break;  
  24.         case 2:  
  25.             scopePredicate = [NSPredicate predicateWithFormat:@"SELF.itemImagePath contains[c] %@",searchText];  
  26.             dataArr =[NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]];  
  27.             break;  
  28.     }  
  29. }  
//自定義搜索方法,根據關鍵詞從allDataArr中搜索到滿足搜索條件的元素,并將匹配的數組賦值給dataArr,由于dataArr是表視圖的數據源,因此表視圖的記錄也會隨之改變。- (void)filterContentForSearchText:(NSString*)searchText scope:(NSUInteger)scope;{    if([searchText length]==0)    {        //查詢所有        dataArr = [NSArray arrayWithArray:allDataArr];        NSLog(@"dataArr count = %d",[dataArr count]);        return;    }        NSPredicate *scopePredicate;        switch (scope) {        case 0:            scopePredicate = [NSPredicate predicateWithFormat:@"(SELF.itemName contains[c] %@) OR (SELF.itemImagePath contains[c] %@)",searchText,searchText];            NSLog(@"searchText=%@",searchText);            dataArr =[NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]];            break;        case 1:            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.itemName contains[c] %@",searchText];            dataArr = [NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]];            break;        case 2:            scopePredicate = [NSPredicate predicateWithFormat:@"SELF.itemImagePath contains[c] %@",searchText];            dataArr =[NSArray arrayWithArray:[allDataArr filteredArrayUsingPredicate:scopePredicate]];            break;    }}

 6.修改cellForRowAtIndexPath方法

Cpp代碼 復制代碼 收藏代碼
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString *CellIdentifier = @"myTableCell";  
  4.     MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  5.     //add code begin:important,for showing searching results  
  6.     //不對cell進行空值的判斷,會導致在搜索時,找不到對應identifier的cell而報錯。  
  7.     if (cell == nil) {  
  8.     //搜索結果采用簡單表視圖cell的style,并非自定義的表視圖cell的style      
  9.     cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];  
  10.         NSUInteger row = [indexPath row];  
  11.         NSDictionary *rowDict = [dataArr objectAtIndex:row];  
  12.         cell.textLabel.text =  [rowDict objectForKey:@"itemName"];  
  13.         NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];  
  14.         cell.imageView.image =  [UIImage imageNamed:imagePath];  
  15.     }  
  16.     //add code end  
  17.   
  18.     NSUInteger row = [indexPath row];  
  19.     NSDictionary *rowDict = [dataArr objectAtIndex:row];  
  20.     cell.label.text =  [rowDict objectForKey:@"itemName"];  
  21.     NSLog(@"cell.label.text =  %@",[rowDict objectForKey:@"itemName"]);  
  22.       
  23.     NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];  
  24.     cell.image.image = [UIImage imageNamed:imagePath];  
  25.     NSLog(@"cell.image.image  =  %@",imagePath);  
  26.       
  27.     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  28.       
  29.     return cell;  
  30. }  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellIdentifier = @"myTableCell";    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    //add code begin:important,for showing searching results    //不對cell進行空值的判斷,會導致在搜索時,找不到對應identifier的cell而報錯。    if (cell == nil) {    //搜索結果采用簡單表視圖cell的style,并非自定義的表視圖cell的style        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];        NSUInteger row = [indexPath row];        NSDictionary *rowDict = [dataArr objectAtIndex:row];        cell.textLabel.text =  [rowDict objectForKey:@"itemName"];        NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];        cell.imageView.image =  [UIImage imageNamed:imagePath];    }    //add code end    NSUInteger row = [indexPath row];    NSDictionary *rowDict = [dataArr objectAtIndex:row];    cell.label.text =  [rowDict objectForKey:@"itemName"];    NSLog(@"cell.label.text =  %@",[rowDict objectForKey:@"itemName"]);        NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];    cell.image.image = [UIImage imageNamed:imagePath];    NSLog(@"cell.image.image  =  %@",imagePath);        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;        return cell;}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 免费国产成人高清在线看软件 | 黄色网址在线播放 | 999精品国产 | 好吊一区二区三区 | 依依成人综合 | 欧美色爱综合 | 精品乱码久久久久 | 国产一区成人 | 91成人影库 | 日韩一级免费毛片 | 国产99视频精品免视看9 | 久久男人视频 | 欧美精品成人一区二区三区四区 | 九九热在线精品视频 | 久久久久久久久久久高潮一区二区 | 自拍偷拍999 | 久在线观看福利视频69 | 国产一精品一av一免费爽爽 | 一级做a爱片性色毛片 | 日本中文字幕高清 | 亚洲精品成人在线视频 | 国产精品久久久久影院老司 | 日韩视| 51国产偷自视频区视频小蝌蚪 | 久久久久久久久久久久久久国产 | 黄色特级毛片 | 今井夏帆av一区二区 | 久久精品欧美视频 | 九九视频精品在线观看 | 成人免费毛片片v | 九一免费在线观看 | 人人舔人人插 | 免费中文视频 | 国产精品资源手机在线播放 | 一区二区三区国产在线 | 97中文字幕第一一一页 | 高清视频91| 国产男人的天堂 | 91精品国产日韩91久久久久久360 | 国产毛片视频 | 国产日韩亚洲 |