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

Shi*_*mar 5 uitableview ios swift

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

下面是我的代码

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) ->  UISwipeActionsConfiguration?  {

    let renameAction  = contextualToggleRenameAction(forRowAtIndexPath: indexPath)
    let lastResAction = contextualToggleLastResponseAction(forRowAtIndexPath: indexPath)

    let swipeConfig = UISwipeActionsConfiguration(actions: [renameAction, lastResAction])
    swipeConfig.performsFirstActionWithFullSwipe = false
    return swipeConfig
}

func contextualToggleLastResponseAction(forRowAtIndexPath indexPath: IndexPath) -> UIContextualAction {
    let sensorData = sensorsList?[indexPath.row]
    var lastResponse = ""
    if sensorData != nil{
        if let lstRes = sensorData!["last_response"] as? String{
            lastResponse = lstRes
        }
    }
    let action = UIContextualAction(style: .normal, title: lastResponse) { (contextAction: UIContextualAction, sourceView: UIView, completionHandler: (Bool) -> Void) in
        print("Last Response Action")
    }
    action.backgroundColor = UIColor(red: 61/255, green: 108/255, blue: 169/255, alpha: 1.0)
    return action
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*lff 5

天哪,我解决了这个愚蠢的问题。

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

但...

对这个 SO 答案大喊大叫,这几乎直接引导我走向成功(见这里)。

关键:在您的 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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)