xcode - XCode- objective-c :删除表行崩溃

标签 xcode uitableview edit delete-row

欢迎!我整个上午都在工作,但是没有香蕉。也许您有见识!

我正在使用标准的NSMutableArray:

self.itemTable = [[NSMutableArray alloc]
                 initWithObjects:@"House.",
                 @"Car.",
                 @"Keys.", nil];

现在,当我在导航栏中按“编辑”并删除一行时,我得到以下错误消息

由于未捕获的异常而终止应用程序
“NSInternalInconsistencyException”,原因:“无效的更新:无效
第0节中的行数。
更新(3)之后的现有部分必须等于
更新(3)之前该部分中包含的行,正负
从该部分插入或删除的行数(已插入0,
1个已删除),加上或减去移入或移出的行数
该部分(移入0,移出0)。”

我将此代码用于“删除”命令:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {


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

}

关于如何使删除行功能起作用的见解?

编辑:

找到了:在工作中放置以下代码:
[self.itemTable removeObjectAtIndex:indexPath.row];

这使得删除代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {

[self.itemTable removeObjectAtIndex:indexPath.row];

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

}

最佳答案

我遇到了同样的问题。解决后找到解决方案。在这里,我的崩溃实际上是由于从数组中删除项目后调用tableview reload引起的。在崩溃报告中,它提到在编辑/更新tableview之前和之后节中的行必须相等。

波纹管代码工作正常:

 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
     if (editingStyle == UITableViewCellEditingStyleDelete)
     {
        //add code here for when you hit delete
        [self deleteItemAtRowIndex:indexPath];
    }
}


-(void)deleteItemAtRowIndex:(NSIndexPath *)indexpath
{
[pTableDataDataArray removeObjectAtIndex:indexpath.row];

[pTableView beginUpdates];
//[self LoadTempletesData];    no need to reload, simply remove row that to is to be delete
[pTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexpath] withRowAnimation:UITableViewRowAnimationNone];
//make changes
[pTableView endUpdates];
}

希望对别人有帮助

关于xcode - XCode- objective-c :删除表行崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10198284/

相关文章:

java - 用于编辑 ArrayList 中对象的 boolean 方法

javascript - 更新sql失败

ios - 只有将数据完全存储在核心数据中后,才如何调用数据datafetch函数?

iOS : How to override the button action of UIWebView and call our own Objective C method

android - Phonegap构建在phonegap应用程序开发中的作用是什么

ios - 使用它的 indexPath.row 更改单元格图像

ios - Xcode 7 IOS 9 Google AdMob 跟踪

ios - Xamarin iOS GetCell 动态拉伸(stretch) cellView

ios - 将 UIView 添加到 TableViewfooter 不完全可见

JavaFX 可编辑单元格,焦点更改为不同的填充单元格