使用UISearchDisplayController时显示搜索结果会崩溃

时间:2014-03-14 17:55:35

标签: ios uitableview uisearchdisplaycontroller

我尝试使用CoreData为我的TableView实现SearchBarView控制器。但是,当我点击搜索栏时,应用程序崩溃,我收到以下错误:

  

断言失败 - [UISearchResultsTableView   dequeueReusableCellWithIdentifier:forIndexPath:],   /SourceCache/UIKit/UIKit-2935.137/UITableView.m:5439
  ***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法将单元格出列   带标识符Cell - 必须注册一个nib或类   标识符或连接故事板'

中的原型单元格

没有搜索栏我会像这样显示我的TableContent:

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

    // Configure the cell...
    NSManagedObject *device = [self.inventory objectAtIndex:indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"productName"]]];
    //[cell.detailTextLabel setText:[device valueForKey:@"company"]];

    return cell;
}

完美无缺。

所以现在我试图像这样展示SearchbarViewControllerContent:

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

    // Configure the cell...
    NSManagedObject *device = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView)
        {device = [searchResults objectAtIndex:indexPath.row];}
    else
        {device = [self.cart objectAtIndex:indexPath.row]; }

    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"productName"]]];

    return cell;
}

但这不起作用。

如果你必须知道我的搜索栏(应该)如何工作:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 71;
}

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

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

    return YES;
}

1 个答案:

答案 0 :(得分:0)

您必须在viewDidLoad方法中注册搜索tableView的单元格,例如:

  [self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:<Nib Name> bundle:nil] forCellReuseIdentifier:<cellIdentifier>]