UITableView:滑动RowAction 取消选中的行 [英] UITableView: swiping RowAction cancels the selected rows

查看:23
本文介绍了UITableView:滑动RowAction 取消选中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在已弃用的 UITableViewRowAction 类和 UISwipeActionsConfiguration 类中看到这种行为:

I can see this behavior in both deprecated UITableViewRowAction class and UISwipeActionsConfiguration class:

如果您将 allowsMultipleSelection 属性设置为 true,并且假设您选择了 3 行:

If you have allowsMultipleSelection property set to true and, let's say, you have 3 rows selected:

当您开始为 RowAction 滑动表格中的任何行时,先前选择的行(全部 3 个)将不突出显示,并且 indexPathsForSelectedRows 属性变为 nil.

When you start swiping any row in the table for a RowAction the previously selected rows -- all 3 of them -- become unhighlighted, and the property indexPathsForSelectedRows drops to nil.

  • 这种行为有意义吗?
  • 是否有任何取消选择"回调(因为我正在显示所选行的数量)
  • 保留所选行数组的可能解决方法是什么?

推荐答案

UITableView 在您滑动表格中的一行时进入编辑模式.这是你的

UITableView enters editing mode when you swipe a row in the table. This is your

'取消选择'回调

您可以在进入模式时备份所选行并在退出时恢复:

You can backup your selected rows on entering the mode and restore on exiting:

class ViewController: UITableViewController {

    var indexPathsForSelectedRows: [IndexPath]?

    override func setEditing(_ editing: Bool, animated: Bool) {
        if editing {
            indexPathsForSelectedRows = tableView.indexPathsForSelectedRows
        } else {
            indexPathsForSelectedRows?.forEach { tableView.selectRow(at: $0, animated: false, scrollPosition: .none) }
        }
        super.setEditing(editing, animated: animated)
    }
}

另请注意,如果您在编辑期间重新排列/删除/插入行,您需要相应地更新您存储的 indexPathsForSelectedRows 以便您恢复正确的索引路径.

Also note that if you re-arrange/delete/insert rows during editing, you'll need to update your stored indexPathsForSelectedRows accordingly so you restore correct index paths.

这篇关于UITableView:滑动RowAction 取消选中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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