iOS Swift:在deleteRowsAtIndexPaths上崩溃

col*_*unn 4 uikit ios icloud swift cloudkit

当我从tableView中删除一行时,我遇到了崩溃.不知道发生了什么事.这是我的代码:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        let items = days[indexPath.row]
        self.removeItems(items, indexPath: indexPath)
    }

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return days.count
}
Run Code Online (Sandbox Code Playgroud)

为removeItems

func removeItems(items: [CKRecord], indexPath: NSIndexPath) {

    for item in items {
        db.deleteRecordWithID(item.recordID) { (record, error) -> Void in
            if error != nil {
                println(error.localizedDescription)
            }

            dispatch_async(dispatch_get_main_queue()) { () -> Void in
                if let index = find(exercises, item) {
                    exercises.removeAtIndex(index)
                }
            }
        }
    }

    days.removeAtIndex(indexPath.row)
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*mbo 6

在删除单元格(或添加)之前,您需要调用beginUpdates()所涉及的tableView.然后删除或添加单元格.完成后,打电话endUpdates().一旦你打电话endUpdates().请记住,一旦调用endUpdates()tableView模型,就必须与删除或添加的部分和行数一致.开始和结束更新允许ui为所有单元格更改呈现连贯的唯一动画.

tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)