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

Posted

技术标签:

【中文标题】我使用一个简单的搜索显示控制器【英文标题】:Im using a simple search display controller 【发布时间】:2012-03-08 11:38:06 【问题描述】:

我正在使用一个简单的搜索显示控制器。我收到这个错误,我不知道。这是代码请帮助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;

错误如下:

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 数据源必须从 tableView:cellForRowAtIndexPath: 返回一个单元格:”

【问题讨论】:

您找到解决问题的方法了吗?我遇到了类似的问题,我真的认为那是因为我没有正确地将原型单元格与 searchResultsTableView 链接 【参考方案1】:

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

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

搜索结果无论如何都会显示一个新的 tableview,在这种情况下它无法识别以前的标识符,因为旧的 tableview 不在问题中。

【讨论】:

【参考方案2】:

原因是“UITableView 数据源必须从 tableView:cellForRowAtIndexPath: 返回一个单元格”,正如您在提问之前轻轻解释的那样。 因此,当您要求表格视图将可重用单元格出列时,请查看此方法的顶部

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

想想如果表格视图没有早先创建的可免费使用的单元格会发生什么。当你创建你的 UITableViewController 时,它肯定没有任何东西可以提供给你,它是空的。它返回 nil。

因此,如果表格视图无法为您提供“已使用”,请在您要求将其出列并创建“全新”单元格的行下方添加以下行。

if (!cell) 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease]; //if you compile with ARC, drop autorelease

您使用 @"cell" 作为您的单元格 ID,因此请考虑至少创建局部变量 CellIdentifier 以避免重复输入“魔术”字符串。看看Table View Programming guide,因为重用单元格是构建表格视图时最重要的功能。祝你好运!

【讨论】:

【参考方案3】:

前段时间我自己遇到了这个问题,在一个地方修复了它,忘记了它,不得不再次重新发现解决方案。

我的解决方案是使用作为 arg 传递给 tableView:cellForRowAtIndexPath: 的 tableView,而是使用属于封闭 tableViewController 类的 tableView。

在以下示例中,它最近刚刚更改,因此 conditionThatDoesntMatterForThisExample 现在返回的结果与以前略有不同,我发现(或重新发现,如果您愿意)dequeueResuableCellWithIdentifier: 在第一种情况总是,但只有当 tableView 不是第二次调用的 searchResultsTableView 时才返回一个好的单元格。

而答案就在于我的storyboard的tableViewController中的tableView有我想要使用的原型单元格,而searchResultsTableView所代表的tableView却没有!

- (UITableViewCell *)tableView:(UITableView*)tableView
         cellForRowAtIndexPath:(NSIndexPath*)indexPath

    BOOL isSearching = tableView == self.searchDisplayController.searchResultsTableView;
    UITableViewCell* cell;
    BOOL isAnchor = conditionThatDoesntMatterForThisExample;

    if (isAnchor)
    
        cell = [self.tableView dequeueReusableCellWithIdentifier:PlainCell];
    
    else
    
        cell = [tableView dequeueReusableCellWithIdentifier:ReplyCell];
    

【讨论】:

不要使用dequeueReusableCellWithIdentifier:forIndexPath:,它给我带来了问题,不知道为什么。 我猜想 searchDisplay 控制器的 indexPath 不会像您对普通 tableView 所希望的那样严格匹配 indexPath。

以上是关于我使用一个简单的搜索显示控制器的主要内容,如果未能解决你的问题,请参考以下文章

制作一个简单的搜索功能,使光标跳转到(或突出显示)要搜索的单词

iPhone 核心数据驱动搜索:如何,资源?

来自脚本的 Vim 搜索和高亮控制

用于搜索屏幕的 MVVM 和 RxSwift

模态视图导航栏未正确显示

使用 Laravel 返回令牌不匹配的简单 ajax 搜索