iOS swift : Crash on deleteRowsAtIndexPaths

标签 ios swift uikit icloud 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
}

移除元素

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)
}

最佳答案

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

tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
tableView.endUpdates()

关于iOS swift : Crash on deleteRowsAtIndexPaths,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28800707/

相关文章:

ios - iOS 上的 PNG 验证

ios - 如何在 SwiftUI 中引用外部 iOS 系统状态更新?

ios - 将录制的音频上传到 Firebase

ios - 如何阻止我的 UITabBar 在隐藏后响应触摸?

ios - UIButton 触摸 Action 不在触摸时调用但在触摸移动时调用

ios - 无法在 iOS6 中使导航栏完全透明

ios - 需要将 iOS 5.0 App 转换为 iOS 4.3.3

ios - 归档时xcode没有这样的模块错误

json - 在 iOS 7 下尝试使用 SwiftyJSON 解析 JSON 数据时出现未知类型

objective-c - UITableView 一次删除所有行