0

我已经在 tableview 控制器中使用 UISearchdisplaycontroller 实现了 UISearchBar,但是在单击搜索栏时出现以下问题:

-[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;
}
4

1 回答 1

0

您正在使用该dequeueReusableCellWithIdentifier:forIndexPath:方法。根据苹果文档,您必须在调用此方法之前使用registerNib:forCellReuseIdentifier:or方法注册一个类或 nib 文件。registerClass:forCellReuseIdentifier:您尚未为重用标识符注册 nib 或类"TransitionCell"

根据您的代码,您似乎希望 dequeue 方法在nil没有单元格给您的情况下返回。您需要使用该dequeueReusableCellWithIdentifier:行为:

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

    // Configure common elements here

}
于 2017-02-16T16:40:26.203 回答