我在删除表格视图中的单元格时遇到问题。似乎每当删除一个单元格时,屏幕上显示的下一个单元格就不会显示。我已经调试cellForRowAt
并且单元格正在出列和数据集,但表格视图单元格从未呈现。这是我的相关代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "taskCell", for: indexPath) as! TaskTableViewCell
if let task = self.task(indexPath: indexPath) {
cell.update(task: task)
}
cell.checkChanged = ...
return cell
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
guard let task = self.task(indexPath: indexPath) else {
return nil
}
var actions = [UIContextualAction]()
let delete = UIContextualAction(style: .destructive, title: LocStr("delete")) { [unowned self] (action, view, success) in
self.delete(indexPath: indexPath)
success(true)
}
actions.append(delete)
...
let configuration = UISwipeActionsConfiguration(actions: actions)
return configuration
}
func delete(indexPath: IndexPath) {
DispatchQueue.main.async { [unowned self] in
if let task = self.task(indexPath: indexPath) {
self.allTasks.removeAll(where: { $0.id == task.id })
self.sort()
...
self.tableView.beginUpdates()
self.tableView.deleteRows(at: [indexPath], with: .fade)
self.tableView.endUpdates()
}
}
}
如您所见,当我滚动表格视图时,即使为该单元格调用了下一个单元格也是空的cellForRowAt
。我调试了 UI 并能够确认空白区域中没有单元格。
其他人遇到过这个问题吗?我在这里做错了吗?
我找不到这方面的官方文档,但我相信您tableView.deleteRows
在使用.destructive
样式时不应该调用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句