新建navigation-based project。打開.xib文件,拖一個search bar and search displaycontroller 對象到table view對象上方,如下圖所示,選中file's owner ,打開connections面板:
現在我們來創建search bar和searchdisplay controller的出口。打開assistant editor,按住ctrl鍵,將searchdisplay controller拖到viewcontroller 的頭文件中。創建一個名為searchdisplaycontroller的出口,然后點connect。
同樣的方法為search bar創建連接。現在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。一個用于作為數據源,一個用于保存查找結果。在本文中,我使用字符串數組作為數據源。繼續編輯.m文件前,別忘了synthesize相關屬性:
-
@synthesize searchdisplaycontroller;
-
@synthesize searchbar;
-
@synthesize allitems;
-
@synthesize searchresults;
在viewdidload 方法中,我們構造了我們的字符串數組:
- (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行數的方法中,我們先判斷當前table view是否是searchdisplaycontroller的查找結果表格還是數據源本來的表格,然后返回對應的行數:
- (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;
}
現在來實現當搜索文本改變時的回調函數。這個方法使用謂詞進行比較,并講匹配結果賦給searchresults數組:
- (void)filtercontentforsearchtext:(nsstring*)searchtext scope:(nsstring*)scope {
nspredicate *resultpredicate = [nspredicate predicatewithformat:@"self contains[cd] %@", searchtext];
self.searchresults = [self.allitems filteredarrayusingpredicate:resultpredicate];
}
接下來是uisearchdisplaycontroller的委托方法,負責響應搜索事件:
#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;
}
運行工程,當你在搜索欄中點擊及輸入文本時,如下圖所示:
uisearchdisplaycontroller 點擊搜索出現黑條問題解決方案
如果點擊按鈕啟動 presentviewcontroller 的時候出現下圖效果:
比如說我這里現在代碼式這樣寫的:
addfriendviewcontroller *addfriendvc = [[addfriendviewcontroller alloc] init];
uinavigationcontroller *nav =[[uinavigationcontroller alloc] initwithrootviewcontroller:addfriendvc];
[self presentviewcontroller:nav animated:yes completion:nil];
[addfriendvc release];
[nav release];
發現問題所在 uinavigationcontroller 的背景顏色是黑色的;
為了解決tableview點擊搜索出現的黑條:
代碼:
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)];
效果: