【问题标题】:Displaying search results crashes when using UISearchDisplayController使用 UISearchDisplayController 时显示搜索结果崩溃
【发布时间】:2014-03-14 17:55:34
【问题描述】:

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

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

如果没有搜索栏,我会像这样显示我的 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;
}

【问题讨论】:

  • 同样的问题,但方法不同
  • 还有其他解决方案吗?
  • “另一种解决方案”是什么意思?您从重复问题的答案中尝试了哪些解决方案?为什么它们不起作用?
  • 对不起...好吧,所以我通过重复的主题工作并解决了应用程序崩溃的问题。但是当我在搜索栏中输入一个字母时,返回“无结果”。我如何确定搜索栏正在我的 CoreData 中搜索?还是与此“无结果”-错误无关?

标签: ios uitableview uisearchdisplaycontroller


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    相关资源
    最近更新 更多