0

我有带有 searchDisplayController 的表格视图。当我搜索一些文本时,我成功地在屏幕上显示了结果。当我想在现有结果中添加一个单元格时,我的问题就出现了。当 searchDisplayController 处于活动状态时,必须添加一个单元格。

那是:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSMutableDictionary * dict ;
        BOOL isSearchView = tableView == self.searchDisplayController.searchResultsTableView;
        if (isSearchView) {
            dict = [searchTasks objectAtIndex:indexPath.row];
            [self.searchDisplayController.searchResultsTableView deselectRowAtIndexPath:indexPath
                                                                               animated:YES];
        }
        else{
            dict = [self.tasks objectAtIndex:indexPath.row];
            [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
            Task * task = [self getsomeTask];
            NSIndexPath *  ip = [NSIndexPath indexPathForRow:indexPath.row+1  
inSection:indexPath.section];
            if (isSearchView) {
                [searchTasks insertObject:task atIndex:ip.row];
                [self.searchDisplayController.searchResultsTableView  
                                        insertRowsAtIndexPaths:@[ip]
                                        withRowAnimation:UITableViewRowAnimationRight];

    }

在执行此之后,在最后一行之后insertRowsAtIndexPaths:@[ip]

代码执行:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchDisplayController.isActive) {
        return [searchTasks count];
    } else {
        return [tasks count];
    }
}

这很好,在这里它根据程序逻辑选择第一个正确的选项。但是它崩溃了,在执行此操作后它永远不会调用

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

UITableview 的委托函数。

并给我错误:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

我无法理解它在 rowcount 函数之后不调用 cellForRowAtIndexPath 函数的原因。

有什么建议么?

4

0 回答 0