0

我在的tableview控制器實現的UISearchBar與UISearchdisplaycontroller,但我發現了以下問題點擊搜索欄:iOS版:的UISearchBar與UISearchdisplaycontroller墜毀通過點擊搜索欄

斷言失敗 - [UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath :], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.5.2/UITableView

我設置了所有委託方法和使用下面的代碼:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"projectName contains[c] %@", searchText]; 

     _searchResults = [_searchResults filteredArrayUsingPredicate:resultPredicate]; 

} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
             objectAtIndex:[self.searchDisplayController.searchBar 
                selectedScopeButtonIndex]]]; 

    return YES; 
} 
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView { 

    [tableView setContentInset:UIEdgeInsetsMake(100, 0, 0, 0)]; 


} 

代碼cellForRowAtIndexPath是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"TransitionCell"; 

    METransitionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    if (cell == nil) { 
     cell = [[METransitionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     // Configure common elements here 

    } 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     //activeProject = [_searchResults objectAtIndex:indexPath.row]; 
    } else { 


    } 


    NSString *transition = @"Test"; 

    cell.cupponTitle.text = transition; 
    cell.favourtiesButton.tag = indexPath.row; 
    [cell.favourtiesButton addTarget:self action:@selector(favourtiesClicked:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 

回答

0

您正在使用的dequeueReusableCellWithIdentifier:forIndexPath:方法。根據蘋果文檔,您必須在調用此方法之前使用registerNib:forCellReuseIdentifier:registerClass:forCellReuseIdentifier:方法註冊類或筆尖文件。 您尚未註冊重複標識符"TransitionCell"的筆尖或類。

根據你的代碼,你似乎希望如果它沒有一個單元格給你,那麼出列方法返回nil。您需要使用dequeueReusableCellWithIdentifier:該行爲:

METransitionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[METransitionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    // Configure common elements here 

} 
+0

,但如果我們點擊它的UISearchBar崩潰..我怎麼給CellIdentifier到UISearchDisplayController? –

+0

更新了代碼,請更新您的tableview cellForRowAtIndexPath方法 –

相關問題