1
編輯:感謝您的答案如下。我無法讓UISearchDisplayController
重新加載表格,所以當搜索文本發生變化並更改用於填充表格的數組時,我最終調用了篩選器方法。cellForRowAtIndexPath不從UISearchDisplayController代理調用
我已經添加了UISearchDisplayController
到UITableView
編程如下(這是使用的cocos2d)。 cellForRowAtIndexPath
最初在視圖加載時調用,但當搜索委託返回YES
時不調用cellForRowAtIndexPath
。
(self
是UIViewController
)
table = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 80.0f, 480.0f, 260.0f)];
table.delegate = self;
table.dataSource = self;
[[[CCDirector sharedDirector] openGLView] addSubview:table];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[searchBar sizeToFit];
searchBar.delegate = self;
searchBar.placeholder = @"Search";
table.tableHeaderView = searchBar;
UISearchDisplayController *searchDC = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDC.delegate = self;
searchDC.searchResultsDataSource = self;
searchDC.searchResultsDelegate = self;
// [searchBar release];
// [searchDC release];
當輸入到搜索欄的字符,下面的代表們呼籲,numberOfRowsInSection
叫,但cellForRowAtIndexPath
不會被調用。
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
// Do filtering and add to the search array
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:[[controller.searchBar scopeButtonTitles] objectAtIndex:[controller.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[controller.searchBar text] scope:[[controller.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
首先,請檢查表格的結果數組是否不爲空。其次,還要確保你從搜索欄代理調用'[table reloadData];'。 – 2012-02-27 06:26:48