gpt4 book ai didi

iOS Swift - TableView 删除行错误

转载 作者:行者123 更新时间:2023-11-29 12:06:26 25 4
gpt4 key购买 nike

错误:'无效更新:第 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)
}

关于iOS Swift - TableView 删除行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34823930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com