iOS Swift - TableView Delete Row Bug



错误:'无效更新:第 0 节中的行数无效。 更新后现有节中包含的行数 (1) 必须等于更新前该节中包含的行数 (1),加上或减去从该节插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该节的行数(0 移入, 0 搬出)。

查看许多相关帖子,我仍然找不到答案。

以下是相关的代码片段(不包含所有代码):

var todoItems : [Assignment] = []
var sections = Dictionary<String, Array<Assignment>>()
var sortedSections = [String]()
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == UITableViewCellEditingStyle.Delete {
        var tableSection = sections[sortedSections[indexPath.section]]
        tableSection!.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sortedSections[section]
}
override func viewDidLoad() {
    super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return sections.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections[sortedSections[section]]!.count
}

感谢您的帮助!

您删除了数组中的元素,但缺少带有部分的字典更新:(

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == UITableViewCellEditingStyle.Delete {
        var tableSection = sections[sortedSections[indexPath.section]]
        tableSection!.removeAtIndex(indexPath.row)
        //add this line
        sections[sortedSections[indexPath.section]] = tableSection
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}

}

最新更新