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方法 –