【问题标题】:Crash when deleting row - Inconsistency in section not being updated删除行时崩溃 - 部分不一致未更新
【发布时间】:2011-05-13 17:32:41
【问题描述】:

当我使用 commitEditingStyle 从 UITableView 中删除一行时,我的应用程序崩溃并显示以下错误消息。奇怪的是我从第 3 节中删除。根据消息的不一致来自第 4 节。

* -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-1262.60.3/UITableView.m:920 2010-11-22 19:56:44.789 bCab[23049:207] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 4 节中的行数无效。包含的行数在更新后的现有节中 (1) 必须等于更新前该节中包含的行数 (0),加上或减去从该节插入或删除的行数(0 插入,0 删除)。 '

在数据源中,我根据第 3 节中的行数更新第 4 节。当从第 3 节中删除一行时,第 4 节中的行数从 0 变为 1。这似乎导致了问题。有没有办法避免这种情况?

任何指针将不胜感激。 谢谢。

更新

numberOfSectionsInTableView - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 返回 6;
}

numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
bCabAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    if (section == 0) {        // First name, last name
        return 2;
    }
    else if (section == 1) {   // Password
        return 2;   
    }
    else if (section == 2) {   // Mobile, DOB , Gender
        return 3;
    }
    else if (section == 3) {    // credit cards
        return [creditCards count]; 
    }
    else if (section == 4) {    // Add credit card 
        if ([creditCards count] >= 3) {
            return 0;   
        }
        else {
            return 1;   
        }
    }
    else if (section == 5) {
        return 0;   
    }

    return 0;

}

commitEditingStyle

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (indexPath.section == 3) {
        // Delete the row from the data source  
        NSLog(@"%d %d", indexPath.section, indexPath.row);
        [creditCards removeObjectAtIndex:indexPath.row];

        // Delete from backend

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

        //[tableView reloadData];
        }
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }  

}

我尝试过使用和不使用 [tableView reloadData] 的结果相同。

感谢您的帮助!

【问题讨论】:

  • 你能发布你的 numberOfSectionsInTableView 和 tableView:numberOfRowsInSection 方法吗?
  • 你能发布删除行的代码吗?

标签: iphone objective-c uitableview


【解决方案1】:

当第 3 节中的一行被删除时,如何在第 4 节中添加新行?
这不是一个好的解决方案,但是您可以尝试在删除行时重新加载数据。

【讨论】:

  • 系统中信用卡的最大数量是3。如果是3,我不需要添加信用卡单元格。否则,我有它。我试过reloadData,没有效果。感谢您的回复。
【解决方案2】:

在删除单元格之前,是否删除了 creditCards 中对应的对象?

【讨论】:

  • 是的,我正在从相应的数组中删除它。我已经用 commitEditingStyle 代码更新了问题。
  • 在删除行之前尝试重新加载数据?
  • 它再次崩溃,但消息略有不同 - 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 3 节中的行数无效。包含在更新后的现有节 (3) 必须等于更新前该节中包含的行数 (3),加上或减去从该节插入或删除的行数(0 插入,1 删除)。'
【解决方案3】:

在数据源中,我更新了第 4 节 取决于行数 第 3 节. 当一行被删除时 第 3 节,第 4 节中的行数 从 0 到 1。这似乎导致 问题。有没有办法避免 这个?

使用deleteRowsAtIndexPaths:withAnimation 时,您保证数据源将仅删除具有指定索引路径的行。在您的情况下,您还在表中插入一行,这意味着数据源的状态不是表视图所期望的。

当删除第 3 节中的一行时,还涉及在第 4 节中插入一行,您必须执行以下操作:

[self.tableView beginUpdates];
[self.tableView [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPath:[NSArray arrayWithObject:indexPathForInsertedRow] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多