【发布时间】:2018-09-26 16:43:03
【问题描述】:
我在 CoreData 中有一本字典和产品:
var productSortArray: [Date:[Product]?] = [:]
var productArray = [Product]()
这是我的 numberOfRowsInSection:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (productSortArray[dateArray[section]]!!.count)
}
当我在提交编辑样式中删除行时,我更新:
self.productArray.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.reloadData()
但是,删除行重载表时,行数不对,出现问题:
更新后现有节中包含的行数 (1) 必须等于更新前该节中包含的行数 (1),加上或减去从该节中插入或删除的行数(0 插入,1 删除)加上或减去移入或移出该部分的行数(0 移入,0 移出)。'
【问题讨论】:
-
这令人困惑。您显示两个属性
productSortArray和productArray。但是你的numberOfRowsInSection是基于productSortArray和dateArray。最后,当您删除一行时更新productArray。这些都不匹配。 -
可能不相关但从不在
insertRows/deleteRows之后立即致电reloadData()。你摆脱了动画,insertRows/deleteRows确实更新了 UI。 -
dateArray 是日期数组:(
-
在这种情况下如何删除元素字典?谢谢兄弟。
-
您可能必须删除
productSortArray[dateArray[indexPath.section]]!!.remove(at: indexPath.row)。我强烈建议为此设计使用class(带有引用语义)。
标签: swift uitableview