我正在尝试使用 TrailingSwipeActionsConfigurationForRowAt 函数从 UITableView 中删除行。
该行被删除并消失。这部分还可以。
但是接下来出现在屏幕上的行或者当我向下滑动与已删除的行相同的量时甚至不会加载到 tableView 上。
![img1] https://poirot.deus4.com/photo_2019-06-12_16-44-01.jpg
![img2] https://poirot.deus4.com/photo_2019-06-12_16-43-56.jpg
![img3] https://poirot.deus4.com/photo_2019-06-12_16-43-49.jpg
![img4] https://poirot.deus4.com/photo_2019-06-12_16-43-38.jpg
[视频] https://poirot.deus4.com/RPReplay_Final1560345600.mp4
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
}
最佳答案
您展示的代码显然有效,因此问题出在其他地方。
如果没有其他代码,我怀疑问题是因为 TableView 单元格被重复使用:当您滚动 TableView 时,某些单元格会滚动到 View 之外并且可以重复使用。滚动的单元格要么是重复使用的单元格,要么是新的单元格。
无论如何,必须使用表格 View 数据源函数 tableView(_:cellForRowAt:)
来配置显示的单元格。
我觉得您没有在 tableView(_:cellForRowAt:)
中配置单元格。
如果是这样,重复使用的单元格看起来就像滚动出来时一样,但新单元格只是空白。
因此,我建议检查您是否确实正确配置了 tableView(_:cellForRowAt:)
中的所有单元格。
关于ios - 使用 TrailingSwipeActionsConfigurationForRowAt 删除行时奇怪的缺少其他行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563828/