我正在尝试使用trailingSwipeActionsConfigurationForRowAt函数从UITableView中删除行。
该行将被删除并消失。这部分没问题。
但是,下一行将要出现在屏幕上,或者当我向下滑动与删除的行相同数量的行时,甚至不会加载到tableView上。
(https://poirot.deus4.com/photo_2019-06-12_16-44-01.jpg)
(https://poirot.deus4.com/photo_2019-06-12_16-43-56.jpg)
(https://poirot.deus4.com/photo_2019-06-12_16-43-49.jpg)
(https://poirot.deus4.com/photo_2019-06-12_16-43-38.jpg)
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredProducts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: .cellID, for: indexPath) as! ProductTableCell
cell.backgroundColor = .red
return cell
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let hide = UIContextualAction(style: .destructive, title: "Hide") { action, view, completion in
self.filteredProducts.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
completion(true)
}
hide.image = #imageLiteral(resourceName: "hdie_product")
hide.backgroundColor = .midGrey
let conf = UISwipeActionsConfiguration(actions: [hide])
return conf
}
发布于 2019-06-13 14:23:59
问题是我没有在TablewViewCell内部调用super.prepareForReuse()
override func prepareForReuse() {
super.prepareForReuse() // this was missing
}
发布于 2019-06-13 04:37:51
您展示的代码显然是有效的,因此问题出在别处。
在没有其他代码的情况下,我怀疑问题是因为表格视图单元格被重用了:当您滚动表格视图时,一些单元格滚动到视图之外,可以重复使用。滚动进入的单元格可以是重复使用的单元格,也可以是新的单元格。
在任何情况下,都必须使用表视图数据源函数tableView(_:cellForRowAt:)
来配置显示的单元格。
您不在tableView(_:cellForRowAt:)
中配置单元,这似乎太像我了。
如果是这样的话,重新使用的单元格在滚动出来时看起来像以前一样,但新的单元格只是空白。所以我建议检查一下你是否真的在tableView(_:cellForRowAt:)
中正确地配置了所有的单元。
https://stackoverflow.com/questions/56563828
复制相似问题