iOS應(yīng)用中UISearchDisplayController搜索效果的用法
新建Navigation-based Project。打開.xib文件,拖一個Search Bar and Search DisplayController 對象到Table View對象上方,如下圖所示,選中File's Owner ,打開Connections面板:

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

同樣的方法為Search Bar創(chuàng)建連接?,F(xiàn)在ViewController的頭文件看起來像這樣:
#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
你可能注意到,我初始化了兩個NSArray。一個用于作為數(shù)據(jù)源,一個用于保存查找結(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ù)組:
- (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ù)源本來的表格,然后返回對應(yīng)的行數(shù):
- (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:方法里,我們需要做同樣的事:
// 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)在來實現(xiàn)當(dāng)搜索文本改變時的回調(diào)函數(shù)。這個方法使用謂詞進(jìn)行比較,并講匹配結(jié)果賦給searchResults數(shù)組:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];
self.searchResults = [self.allItems filteredArrayUsingPredicate:resultPredicate];
}
接下來是UISearchDisplayController的委托方法,負(fù)責(zé)響應(yīng)搜索事件:
#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;
}
運行工程,當(dāng)你在搜索欄中點擊及輸入文本時,如下圖所示:

UISearchDisplayController 點擊搜索出現(xiàn)黑條問題解決方案
如果點擊按鈕啟動 presentViewController 的時候出現(xiàn)下圖效果:

比如說我這里現(xiàn)在代碼式這樣寫的:
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)問題所在 UINavigationController 的背景顏色是黑色的;
為了解決TableView點擊搜索出現(xiàn)的黑條:
代碼:
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的背景色:
[nav.view setBackgroundColor:UIColorFromRGB(0xC6C6CB)];
效果:

相關(guān)文章
iOS定時器的選擇CADisplayLink NSTimer和GCD使用
這篇文章主要為大家介紹了iOS定時器的選擇CADisplayLink NSTimer和GCD使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
iOS UIScrollView和控制器返回手勢沖突解決方法
這篇文章主要介紹了iOS UIScrollView和控制器返回手勢沖突解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
iOS多線程應(yīng)用開發(fā)中使用NSOperation類的基本方法
這篇文章主要介紹了iOS多線程應(yīng)用開發(fā)中使用NSOperation類的基本方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11
iOS頁面跳轉(zhuǎn)及數(shù)據(jù)傳遞(三種)
本文主要介紹了iOS頁面跳轉(zhuǎn)的三種方法及數(shù)據(jù)傳遞的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
IOS App圖標(biāo)和啟動畫面尺寸詳細(xì)介紹
這篇文章主要介紹了IOS App圖標(biāo)和啟動畫面尺寸詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02

