Swift 4 - tableview 设置编辑 false 不会关闭行操作 [英] Swift 4 - tableview set editing false doesn't dismiss row actions

查看:29
本文介绍了Swift 4 - tableview 设置编辑 false 不会关闭行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码 -

    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)
    }

这个函数是从 trailingSwipeActionsConfigurationForRowAt 调用的.

This function is called from 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
}

如您所见,我将编辑模式设置为 false.但是,我的行操作并没有被驳回.

As you can see I'm setting the edit mode to false. However, my row actions are not being dismissed.

我错过了什么?删除正常.但是行操作仍然存在,这会在已删除行的位置留下一个空白区域.该表不会更新以删除该行.但是如果我点击其他任何地方,它会重新绘制表格,一切正常,直到我进行下一次删除.我试过 setNeedsLayout(), layoutIfNeeded() 但没有效果.所以要么这是一个问题,要么是两个问题的结合.请不要建议 reloadData().我知道这会解决所有这些问题,但这不是推荐的方法.

What am I missing? The delete is happening ok. But the row actions remain, and this is leaving a white space in place of where the deleted row was. The table is not updating to remove the row. But if I click anywhere else, it redraws the table and everything is ok until I do the next delete. I've tried setNeedsLayout(), layoutIfNeeded() but no effect. So either this is one issue, or two issues combined. Please don't suggest reloadData(). I know that'll resolve all these issues but that's not a recommended approach.

推荐答案

我遇到了同样的问题,我发现 tableView.setEditing(false, animation: true) 不再需要了.您需要做的就是将 true 传递给 UIContextualAction 处理程序,其余的将被处理.这是我所做的:

I had the same issue and I found that tableView.setEditing(false, animated: true) is not necessary anymore. All you need to do is to pass true to the UIContextualAction handler and the rest will be taken care of. Here is what I do:

@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)
}

这篇关于Swift 4 - tableview 设置编辑 false 不会关闭行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆