这是我的代码 -
func rowActionDelete(indexPath: IndexPath, reminder: Reminder){
do {
self.managedObjectContext.delete(self.listOfSavedItems[indexPath.row] as! NSManagedObject)
try self.managedObjectContext.save()
} catch {
let saveError = error as NSError
print(saveError)
}
self.tableViewItems.setEditing(false, animated: true)
self.listOfSavedItems.remove(at: indexPath.row)
self.tableViewItems.deleteRows(at: [indexPath], with: .automatic)
}
Run Code Online (Sandbox Code Playgroud)
从 TrailingSwipeActionsConfigurationForRowAt 调用此函数。
@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
self.selectedRowReminder = Reminder(withFetchedObject: self.listOfSavedReminders[indexPath.row] as! NSManagedObject)
// Delete
let delete = UIContextualAction(style: .destructive, title: " ") { (action, view, handler) in
self.rowActionDelete(indexPath: indexPath, reminder: self.selectedRowReminder)
}
delete.backgroundColor = .red
delete.image = UIImage(named: "Trash")
// Share
let share = UIContextualAction(style: .destructive, title: " ") { (action, view, handler) in
self.rowActionShare(reminder: self.selectedRowReminder)
}
share.backgroundColor = .gray
share.image = UIImage(named: "Share")
// Edit
let edit = UIContextualAction(style: .destructive, title: " ") { (action, view, handler) in
self.rowActionEdit(reminder: self.selectedRowReminder)
}
edit.backgroundColor = .darkGray
edit.image = UIImage(named: "Edit")
let configuration = UISwipeActionsConfiguration(actions: [delete, edit, share])
return configuration
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我将编辑模式设置为 false。然而,我的行径并没有被驳回。
我缺少什么?删除操作正常。但行操作仍然存在,这会在已删除行所在的位置留下一个空白。该表不会更新以删除该行。但是,如果我单击其他任何地方,它会重新绘制表格,一切正常,直到我进行下一次删除。我尝试过 setNeedsLayout()、layoutIfNeeded() 但没有效果。因此,这要么是一个问题,要么是两个问题的结合。请不要建议 reloadData()。我知道这会解决所有这些问题,但这不是推荐的方法。
编辑:
我遇到了同样的问题,我发现tableView.setEditing(false, animated: true)不再需要了。您所需要做的就是将true其交给UIContextualAction处理程序,其余的将由我们处理。这是我所做的:
@available(iOS 11, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, view, handler) in
self.deleteRow(at: indexPath)
handler(true) // Will do the animation
}
return UISwipeActionsConfiguration(actions: [deleteAction])
}
// Delete
func deleteRow(at indexPath: IndexPath) {
items.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .left)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1060 次 |
| 最近记录: |