我一直在try 向我的应用程序中的TableViewController添加简单的搜索功能.我遵循了雷·温德里希的指导.我有一个带有一些数据的tableView,我在storyboard中添加了搜索栏+显示控制器,然后我有以下代码:

#pragma mark - Table View
     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BreedCell" forIndexPath:indexPath];

        //Create PetBreed Object and return corresponding breed from corresponding array
        PetBreed *petBreed = nil;

        if(tableView == self.searchDisplayController.searchResultsTableView)
            petBreed = [_filteredBreedsArray objectAtIndex:indexPath.row];
        else
            petBreed = [_breedsArray objectAtIndex:indexPath.row];

        cell.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = petBreed.name;

        return cell;
    }

#pragma mark - Search
    -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
        [_filteredBreedsArray removeAllObjects];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchString];
        _filteredBreedsArray = [[_breedsArray filteredArrayUsingPredicate:predicate] mutableCopy];

        return YES;
    }

    -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
        // Tells the table data source to reload when scope bar selection changes

        [_filteredBreedsArray removeAllObjects];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",self.searchDisplayController.searchBar.text];
        _filteredBreedsArray = [[_breedsArray filteredArrayUsingPredicate:predicate] mutableCopy];
        return YES;
    }

标准内容,但是当我在搜索栏中输入文本时,每次都会崩溃,并显示以下错误:

2013-01-07 19:47:07.330 FindFeedo[3206:c07] *** Assertion failure in -[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460
2013-01-07 19:47:07.330 FindFeedo[3206:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier BreedCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

我知道在iOS 6中,单元格的处理和出列系统发生了变化,而且搜索使用了不同的tableView,所以我认为问题在于带有过滤结果的搜索tableView不知道单元格,所以我在视图中放入了以下内容DidLoad:

[self.searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"BreedCell"];

瞧啊!它起作用了...只是在你第一次搜索的时候.如果您返回到原始结果并再次搜索,应用程序会崩溃,并返回相同的错误.我想也许可以把所有的

if(!cell){//init cell here};

cellForRow方法中的内容,但这是否违背了使用dequeueReusableCellWithIdentifier:forIndexPath:method的全部目的?不管怎样,我迷路了.我错过了什么?请帮忙.提前感谢您的宝贵时间(:

亚历克斯.

推荐答案

试着使用self.在dequeueReusableCellWithIdentifier中使用tableView代替tableView:

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

    //Create PetBreed Object and return corresponding breed from corresponding array
    PetBreed *petBreed = nil;

    if(tableView == self.searchDisplayController.searchResultsTableView)
        petBreed = [_filteredBreedsArray objectAtIndex:indexPath.row];
    else
        petBreed = [_breedsArray objectAtIndex:indexPath.row];

    cell.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = petBreed.name;

    return cell;
}

这段代码运行得很好

Note

如果有自定义高度单元格,则不要使用

[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

改用这个吧

[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Ios相关问答推荐

RN 0.63支持iOS ProMotion?

实例方法wait在异步上下文中不可用;请改用TaskGroup;这是Swift 6中的错误''

不使用iOS 17修改器修改SWIFT用户界面视图

带有RadStudio 11.3/12的Mac Mini M2(Sonoma 14.2.1)上的PAServer-测试连接按钮有效,但部署不起作用

当S没有身体时该如何处理

SwiftData在获取值时应用程序崩溃:线程1:EXC_BREAKPOINT(代码=1,子代码=0x101e8303c)

Xcode 15阻止我的应用程序运行:由于实时模式录制的高比率,丢失了1条日志(log)/路标消息?

为什么在Actor内部使用withTaskGroupwork并行运行?

错误地提取问题的答案选项

转换在 NavigationView 中不起作用

使用 Vim 代替(或与)Xcode 进行 iOS 开发

将图像保存到 Documents 目录并检索邮箱附件

改变 UIView 位置的简单方法?

如何关闭情节提要弹出框

UITextView 从文本的底部或中间开始

如何更改uitableview删除按钮文本

UICollectionView flowLayout 没有正确包装单元格

测试目标 X 遇到错误(提前意外退出,操作从未完成 bootstrap - 不会try 重新启动

UIView 中的 safeAreaInsets 在 iPhone X 上为 0

使用 swift 3 在 UIView 上添加阴影