ios - swift TableView deleteRowsAtIndexPaths NSException

标签 ios swift xcode uitableview

我有一个动态的 TableView,想用动画删除行。

我的代码:

struct TableItem {
    let text: String
    let id: String
    let creationDate: String
    let bug: String
    let comments: String
}
var sections = Dictionary<String, Array<TableItem>>()
var sortedSections = [String]()

//Some other Code

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

     let setChecked = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Erledigt" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
          var tableSection = self.sections[self.sortedSections[indexPath.section]]
          let tableItem = tableSection![indexPath.row]

          //Not important HTTP POST Code

          if(success == 1)
                {
                    NSLog("SUCCESS");
                    self.tableView.beginUpdates()
                    tableSection?.removeAtIndex(indexPath.row)
                    self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
                    self.tableView.endUpdates()
                    //self.getChecked()

                } 
     }

     return [setChecked]
}

如果我运行此代码,我会收到以下错误消息:

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

我不知道我做错了什么。

感谢您的帮助。

最佳答案

这就是值类型陷阱

Swift 集合类型是具有值语义的结构,与具有引用语义的类不同。

var tableSection = self.sections[self.sortedSections[indexPath.section]] 行复制了对象并保留 self.sections 不变。

从列表中删除项目后,您必须将数组分配回 self.sections

tableSection?.removeAtIndex(indexPath.row)
self.sections[self.sortedSections[indexPath.section]] = tableSection

关于ios - swift TableView deleteRowsAtIndexPaths NSException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39817292/

相关文章:

ios - Swift xcode 错误 : Thread 1: EXC_BAD_ACCESS (code=2, 地址=0x7ff54b59ff8)

ios - UICollectionView 单元可重用性问题 - swift

ios - AVPlayer 在视频循环时保存在内存中

ios - 在 Swift 中,改编了 AVCaptureVideoDataOutputSampleBufferDelegate,但从未调用 captureOutput

iphone - 如何在 xcode 中调试 Cordova.framework

ios - 在 xcode 中第一次运行时动画卡顿

ios - 我们可以从 iOS 应用程序加载外部包吗?

ios - 如何通过在一定时间后更改图像来创建动画

ios - 当对象 A 将对象 B 作为属性并且对象 B 有包含对象 A 的数组属性时,Swift 中的保留循环?

swift - 推送远程通知