Thursday, 21 March 2019

Swiping on a tableview is deselecting all selected rows trailingSwipeActionsConfigurationForRowAt

Recently implemented trailingSwipeActionsConfigurationForRowAt , where after swiping from right to left showing two options and its working fine. But the problem is when i select multiple rows or single row, after swiping the row/s they are getting deselected. Is there a way to keep the selection even after swiping?

Below is my code

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

    let selectedRows = tableView.indexPathsForSelectedRows
    if (selectedRows != nil && selectedRows!.count > 0) || unPairButton.currentTitle != "Pair" || recordButton.currentTitle != "Record"{
        let swipeAction = UISwipeActionsConfiguration(actions: [])
        return swipeAction
    }

    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
}



from Swiping on a tableview is deselecting all selected rows trailingSwipeActionsConfigurationForRowAt

No comments:

Post a Comment