1

編輯:感謝您的答案如下。我無法讓UISearchDisplayController重新加載表格,所以當搜索文本發生變化並更改用於填充表格的數組時,我最終調用了篩選器方法。cellForRowAtIndexPath不從UISearchDisplayController代理調用


我已經添加了UISearchDisplayControllerUITableView編程如下(這是使用的cocos2d)。 cellForRowAtIndexPath最初在視圖加載時調用,但當搜索委託返回YES時不調用cellForRowAtIndexPath

selfUIViewController

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; 
} 
+0

首先,請檢查表格的結果數組是否不爲空。其次,還要確保你從搜索欄代理調用'[table reloadData];'。 – 2012-02-27 06:26:48

回答

2

只檢查以下幾件事:

1)的tableview委託&數據源是否設置正確。

2)在未來的數據中不是空的。

3)檢查方法numberOfRowsInSection:返回適當的值。

相關問題