在表格视图上滑动是取消选择所有选定的行尾swipeActionsConfigurationForRowAt

时间:2019-03-18 11:12:09

标签: ios swift uitableview

最近实现了members,在从右向左滑动后显示了两个选项,并且工作正常。但是问题是当我选择多行或单行时,在刷了几行之后它们便被取消选择了。刷卡后有没有办法保持选择?

下面是我的代码

trailingSwipeActionsConfigurationForRowAt

4 个答案:

答案 0 :(得分:1)

将滑动视为一项编辑,因此,如果要保持所选状态,可以启用allowsSelectionDuringEditing

tableView.allowsSelectionDuringEditing = true

答案 1 :(得分:1)

依靠indexPathsForSelectedRows并不总是能得到预期的结果。

相反,您应该维护selectedIndexPaths和数组。

下面是一个代码片段,用于演示:

var selectedIndexPaths = [IndexPath]()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if selectedIndexPaths.contains(indexPath) {
       selectedIndexPaths.removeAll { (ip) -> Bool in
        return ip == indexPath
    }else{
       selectedIndexPaths.append(indexPath)
    }
}

答案 2 :(得分:1)

您可以简单地做到这一点:

  1. 在控制器中为所选索引创建数组

    var arrSelectedIndex : [Int] = []
    
  2. didSelect中,

    if arrSelectedIndex.contains(indexPath.row) { // Check index is selected or not
        // If index selected, remove index from array
        let aIndex = arrSelectedIndex.firstIndex(of: indexPath.row)
        arrSelectedIndex.remove(at: aIndex!)
    }
    else {
        // If index not selected, add index to array
        arrSelectedIndex.append(indexPath.row)
    }
    // reload selected row, or reloadData()
    self.tblView.reloadRows([indexPath.row], with: .automatic)
    
  

编辑

  1. trailingSwipeActionsConfigurationForRowAt中,

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
    {
    
        if self.arrSelectedIndex.contains(indexPath.row) {
    
            let action = UIContextualAction(style: .normal, title: "") { (action, view, handler) in
    
            }
            action.backgroundColor = .green
            let configuration = UISwipeActionsConfiguration(actions: [])
            configuration.performsFirstActionWithFullSwipe = false
            return configuration
        }
        let action = UIContextualAction(style: .normal, title: "Selected") { (action, view, handler) in
    
        }
        action.backgroundColor = .green
        let configuration = UISwipeActionsConfiguration(actions: [action])
        configuration.performsFirstActionWithFullSwipe = false
        return configuration
    }
    

输出

enter image description here

答案 3 :(得分:1)

糟糕,我解决了这个愚蠢的问题。

是的,请确保tableView.allowsSelectionDuringEditing = true和tableView.allowsMultipleSelectionDuringEditing = true。

但是...

大声疾呼这个SO答案,这几乎直接导致我走向成功(see here)。

键:在setEditing的实现中重新选择/取消选择行,这是您覆盖的方法。滑动时,UITableView进入编辑模式。

重要说明:如下所示,在您的任何代码之前调用super.setEditing(...),否则可能无法正常工作,或者至少不能完美实现。

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    for i in 0..<your data.count {
        if this item is selected {
            self.tableView.selectRow(at: IndexPath(row: i, section: 0), animated: false, scrollPosition: .none)
        } else {
            self.tableView.deselectRow(at: IndexPath(row: i, section: 0), animated: false)
        }
    }
}
相关问题