1

我和我UITableViewController一起工作时遇到了一个奇怪的问题NSIndexPath。在我的UITableView我已经实现了一个自定义 OptionCell 淡入点击的单元格下方,然后淡出。使用 iOs 4.3,它可以完美运行。

我最近更新到了新的 xCode 和 iOs 6。我更改了项目中的相关点并且一切正常,除了每当我尝试删除 customCell 时出现的崩溃。

它崩溃了:

-[NSIndexPath 行] 中的断言失败

[...]

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“与 UITableView 一起使用的索引路径无效。传递给表视图的索引路径必须恰好包含两个指定节和行的索引。如果可能,请使用 UITableView.h 中 NSIndexPath 上的类别。

我已经对此错误进行了一些研究。我发现了这个问题,检查了上述步骤,但没有帮助。

在调试时,我发现每次检查row. indexPathToDelete它不是 NIL,应该有一个(适用于旧的 iOs 版本)。他们是否在版本之间更改了 NSIndexPath 的某些内容?我已经检查了所有我能想到的...

崩溃前调试器最后一步的屏幕截图

我在以下代码段中标记了错误:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {

        /* if user tapped the same row twice get rid of the OptionCell */
        if([indexPath isEqual:self.tappedIndexPath]){
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }

        /* update the indexpath if needed...*/
        indexPath = [self modelIndexPathforIndexPath:indexPath];

        /* pointer to delete the control cell */
        NSIndexPath *indexPathToDelete = self.controlRowIndexPath;

        /* if same row is tapped twice => clear tapping trackers */
        if([indexPath isEqual:self.tappedIndexPath]){
            self.tappedIndexPath = nil;
            self.controlRowIndexPath = nil;
        }
        /* otherwise update them appropriately */
        else{
            self.tappedIndexPath = indexPath; /* the row the user just tapped. */
            /* Now set the location of where I need to add the option cell */
            self.controlRowIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
        }

        /* all logic is done, lets start updating the table */
        [tableView beginUpdates];

        /* lets delete the control cell, either the user tapped the same row twice or tapped another row */

        if(indexPathToDelete){



/* CRASH APPEARS HERE!: */



            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete]
                                  withRowAnimation:UITableViewRowAnimationRight];
        }
        /* lets add the new control cell in the right place */
        if(self.controlRowIndexPath){
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.controlRowIndexPath]
                                  withRowAnimation:UITableViewRowAnimationLeft];
//          [self.tableView scrollToRowAtIndexPath:self.controlRowIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

        }

        /* and we are done... */
        [tableView endUpdates];
    } else {
        if (self.controlRowIndexPath) {

            indexPath = [self modelIndexPathforIndexPath:indexPath];
            NSIndexPath *indexPathToDelete = self.controlRowIndexPath;
            self.tappedIndexPath = nil;
            self.controlRowIndexPath = nil;
            if(indexPathToDelete){
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPathToDelete]
                                      withRowAnimation:UITableViewRowAnimationFade];
            }
        }

        VerwaltungViewController* verwaltungViewController;
        verwaltungViewController = [[VerwaltungViewController alloc] init];
        [self.navigationController pushViewController:verwaltungViewController animated:YES];
    }
}

编辑: modelIndexPathforIndexPath:

- (NSIndexPath *)modelIndexPathforIndexPath:(NSIndexPath *)indexPath
{
    int whereIsTheControlRow = self.controlRowIndexPath.row;
    if(self.controlRowIndexPath != nil && indexPath.row > whereIsTheControlRow)
        return [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:0];
    return indexPath;
}
4

0 回答 0