欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS應(yīng)用中UISearchDisplayController搜索效果的用法

 更新時(shí)間:2016年02月18日 09:13:57   作者:頤和園  
這篇文章主要介紹了iOS應(yīng)用中UISearchDisplayController搜索效果的用法,包括點(diǎn)擊搜索出現(xiàn)黑條問(wèn)題的解決方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

新建Navigation-based Project。打開.xib文件,拖一個(gè)Search Bar and Search DisplayController 對(duì)象到Table View對(duì)象上方,如下圖所示,選中File's Owner ,打開Connections面板:

201621891219772.jpg (750×420)

現(xiàn)在我們來(lái)創(chuàng)建Search Bar和SearchDisplay Controller的出口。打開Assistant Editor,按住ctrl鍵,將SearchDisplay Controller拖到ViewController 的頭文件中。創(chuàng)建一個(gè)名為searchDisplayController的出口,然后點(diǎn)Connect。

201621891243378.jpg (750×410)

同樣的方法為Search Bar創(chuàng)建連接?,F(xiàn)在ViewController的頭文件看起來(lái)像這樣:

復(fù)制代碼 代碼如下:

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {   

UISearchDisplayController *searchDisplayController;     UISearchDisplayController *searchBar;   

NSArray *allItems;   

NSArray *searchResults;

@property (nonatomic, retain) IBOutlet UISearchDisplayController *searchDisplayController;

@property (nonatomic, retain) IBOutlet UISearchDisplayController *searchBar;

@property (nonatomic, copy) NSArray *allItems;

@property (nonatomic, copy) NSArray *searchResults; 

@end


你可能注意到,我初始化了兩個(gè)NSArray。一個(gè)用于作為數(shù)據(jù)源,一個(gè)用于保存查找結(jié)果。在本文中,我使用字符串?dāng)?shù)組作為數(shù)據(jù)源。繼續(xù)編輯.m文件前,別忘了synthesize相關(guān)屬性:

  • @synthesize searchDisplayController;
  • @synthesize searchBar;
  • @synthesize allItems;
  • @synthesize searchResults;

在viewDidLoad 方法中,我們構(gòu)造了我們的字符串?dāng)?shù)組:

復(fù)制代碼 代碼如下:

- (void)viewDidLoad {

     [super viewDidLoad]; 

     // [self.tableView reloadData];

     self.tableView.scrollEnabled = YES;

      NSArray *items = [[NSArray alloc] initWithObjects:                       @"Code Geass",                       @"Asura Cryin'",                       @"Voltes V",                       @"Mazinger Z",                       @"Daimos",                       nil]; 

     self.allItems = items;

     [items release]; 

     [self.tableView reloadData];

}


在Table View的返回TableView行數(shù)的方法中,我們先判斷當(dāng)前Table View是否是searchDisplayController的查找結(jié)果表格還是數(shù)據(jù)源本來(lái)的表格,然后返回對(duì)應(yīng)的行數(shù):
復(fù)制代碼 代碼如下:

- (NSInteger)tableView:(UITableView *)tableView   numberOfRowsInSection:(NSInteger)section { 

  NSInteger rows = 0;   

  if ([tableView           isEqual:self.searchDisplayController.searchResultsTableView]){     

    rows = [self.searchResults count];

 }else{    

    rows = [self.allItems count];   

 }   

  return rows;

}


在tableView:cellForRowAtIndexPath:方法里,我們需要做同樣的事:
復(fù)制代碼 代碼如下:

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView           cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   static NSString *CellIdentifier = @"Cell";  

   UITableViewCell *cell = [tableView                               dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil) { 

      cell = [[[UITableViewCell alloc]                   initWithStyle:UITableViewCellStyleDefault                   reuseIdentifier:CellIdentifier] autorelease];   

      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

   }  

   /* Configure the cell. */

   if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){   

    cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];

   }else{

       cell.textLabel.text = [self.allItems objectAtIndex:indexPath.row];

   }  

   return cell;

}


現(xiàn)在來(lái)實(shí)現(xiàn)當(dāng)搜索文本改變時(shí)的回調(diào)函數(shù)。這個(gè)方法使用謂詞進(jìn)行比較,并講匹配結(jié)果賦給searchResults數(shù)組:
復(fù)制代碼 代碼如下:

- (void)filterContentForSearchText:(NSString*)searchText                               scope:(NSString*)scope {

   NSPredicate *resultPredicate = [NSPredicate                                      predicateWithFormat:@"SELF contains[cd] %@",                                     searchText];  

   self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate];

}


接下來(lái)是UISearchDisplayController的委托方法,負(fù)責(zé)響應(yīng)搜索事件:
復(fù)制代碼 代碼如下:

#pragma mark - UISearchDisplayController delegate methods

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller  shouldReloadTableForSearchString:(NSString *)searchString {

   [self filterContentForSearchText:searchString                                 scope:[[self.searchDisplayController.searchBar scopeButtonTitles]                                       objectAtIndex:[self.searchDisplayController.searchBar                                                      selectedScopeButtonIndex]]]; 

   return YES;

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller  shouldReloadTableForSearchScope:(NSInteger)searchOption {

   [self filterContentForSearchText:[self.searchDisplayController.searchBar text]                                 scope:[[self.searchDisplayController.searchBar scopeButtonTitles]                                       objectAtIndex:searchOption]]; 

   return YES;

}


運(yùn)行工程,當(dāng)你在搜索欄中點(diǎn)擊及輸入文本時(shí),如下圖所示:

201621891348854.jpg (750×716)


UISearchDisplayController 點(diǎn)擊搜索出現(xiàn)黑條問(wèn)題解決方案
如果點(diǎn)擊按鈕啟動(dòng) presentViewController 的時(shí)候出現(xiàn)下圖效果:

201621891407773.gif (640×774)

比如說(shuō)我這里現(xiàn)在代碼式這樣寫的:

復(fù)制代碼 代碼如下:

AddFriendViewController *addFriendVC = [[AddFriendViewController alloc] init]; 
   UINavigationController *nav =[[UINavigationController alloc] initWithRootViewController:addFriendVC]; 
   [self presentViewController:nav animated:YES completion:nil]; 
   [addFriendVC release]; 
   [nav release]; 

發(fā)現(xiàn)問(wèn)題所在 UINavigationController 的背景顏色是黑色的;
 
為了解決TableView點(diǎn)擊搜索出現(xiàn)的黑條:

代碼:

復(fù)制代碼 代碼如下:

AddFriendViewController *addFriendVC = [[AddFriendViewController alloc] init]; 
    UINavigationController *nav =[[UINavigationController alloc] initWithRootViewController:addFriendVC]; 
    [nav.view setBackgroundColor:UIColorFromRGB(0xC6C6CB)]; 
    [self presentViewController:nav animated:YES completion:nil]; 
    [addFriendVC release]; 
    [nav release]; 

改變了Nav的背景色:
復(fù)制代碼 代碼如下:

[nav.view setBackgroundColor:UIColorFromRGB(0xC6C6CB)];

效果:

201621891433964.gif (642×554)

相關(guān)文章

  • iOS實(shí)現(xiàn)微信朋友圈與搖一搖功能

    iOS實(shí)現(xiàn)微信朋友圈與搖一搖功能

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)微信朋友圈與搖一搖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • ios開發(fā)Flutter構(gòu)建todo?list應(yīng)用

    ios開發(fā)Flutter構(gòu)建todo?list應(yīng)用

    這篇文章主要為大家介紹了ios開發(fā)Flutter構(gòu)建todo?list應(yīng)用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • iOS Xcode創(chuàng)建文件時(shí)自動(dòng)生成的注釋方法

    iOS Xcode創(chuàng)建文件時(shí)自動(dòng)生成的注釋方法

    下面小編就為大家分享一篇iOS Xcode創(chuàng)建文件時(shí)自動(dòng)生成的注釋方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • iOS坐標(biāo)系的深入探究

    iOS坐標(biāo)系的深入探究

    這篇文章主要給大家介紹了關(guān)于iOS坐標(biāo)系的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • iOS 檢測(cè)文本中的URL、電話號(hào)碼等信息

    iOS 檢測(cè)文本中的URL、電話號(hào)碼等信息

    本文主要介紹了iOS 檢測(cè)文本中的URL、電話號(hào)碼等信息的相關(guān)資料。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-04-04
  • iOS中遍歷的方法總結(jié)

    iOS中遍歷的方法總結(jié)

    本篇文章主要介紹了iOS中遍歷的方法總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • 仿IOS的越界回彈效果和左右滑動(dòng)功能

    仿IOS的越界回彈效果和左右滑動(dòng)功能

    本文主要給大家講述了制作一個(gè)仿IOS的越界回彈效果和左右滑動(dòng)功能,簡(jiǎn)易的側(cè)滑菜單控件,對(duì)此有興趣的朋友參考下學(xué)習(xí)下吧。
    2018-02-02
  • iOS 控制器自定義動(dòng)畫跳轉(zhuǎn)方法(模態(tài)跳轉(zhuǎn))

    iOS 控制器自定義動(dòng)畫跳轉(zhuǎn)方法(模態(tài)跳轉(zhuǎn))

    下面小編就為大家分享一篇iOS 控制器自定義動(dòng)畫跳轉(zhuǎn)方法(模態(tài)跳轉(zhuǎn)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • IOS代碼筆記之勾選

    IOS代碼筆記之勾選"記住密碼"整體button

    這篇文章主要為大家詳細(xì)介紹了IOS實(shí)現(xiàn)勾選"記住密碼"整體button效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • IOS使用progssview仿滴滴打車圓形計(jì)時(shí)

    IOS使用progssview仿滴滴打車圓形計(jì)時(shí)

    本文給大家分享的是IOS中實(shí)現(xiàn)仿滴滴打車的原型計(jì)時(shí)效果,非常的實(shí)用,有需要的小伙伴可以參考下。
    2015-07-07

最新評(píng)論