我使用简单的搜索显示控制器

new*_*867 0 objective-c uitableview uisearchbar ios5

我正在使用一个简单的搜索显示控制器.我收到这个错误我不知道.这是代码plsss帮助n谢谢!!!!!

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rows = 0;

    if([tableView isEqual:self.searchDisplayController.searchResultsTableView])
    {
        rows = [self.searchResults count];     
    }
    else
    {
        rows = [self.allItems count];
    }

    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if([tableView isEqual:self.searchDisplayController.searchResultsTableView])
    {
        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
    }
    else
    {
        cell.textLabel.text = [self.allItems objectAtIndex:indexPath.row];
    }

    return cell;
    NSLog(@"the contents of cell are :%@",cell.textLabel.text);
}

-(void) filterContentsForSearchText:(NSString *)searchText scope:(NSString *)scope
{
    NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd]  %@",searchText];
    self.searchResults = [self.allItems filteredArrayUsingPredicate:resultsPredicate];
}


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

    return YES;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *) controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentsForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

错误如下:

2012-03-08 15:37:18.905 SearchViewController [1082:fe03] *断言失败 - [UISearchResultsTableView _createPreparedCellForGlobalRow:withIndexPath:],/ SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:6072 2012-03-08 15: 37:18.907 SearchViewController [1082:fe03]*由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath:'

小智 7

而不是:UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

试试这个:UITableViewCell*cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];

搜索结果无论如何都会呈现一个新的tableview,因此它无法识别先前的标识符,因为旧的tableview不在问题中.