skip to Main Content

I have a searchController and tableView in A ViewController where I can search any item from tableView and select that item. I’m saving all selected items in other ViewController B.
Now when I again redirect to A VC where I can see my selected items, I want to deselect some items from selected cells, but I’m not able to deselect the selected cell all other deselected cells I can select without a problem.
When I tap on the selected cell to deselect, I can see it is not calling the below method.

tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)

In my viewDidLoad I have the following properties

tableView.isEditing = true
tableView.allowsMultipleSelectionDuringEditing = true

And I’m not using any custom cells. Please help me if you have any solution. TIA.

2

Answers


  1. Chosen as BEST ANSWER

    I have resolved this issue the problem was related to the way I was setting the cell selection I have added the following line in the cellForRow method.

        if cellItem.isSelected {
           tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
        } else {
           tableView.deselectRow(at: indexPath, animated: true)
        }
    

  2. You can control the selection using your table view data source model.

    assume you have an array of objects ex: var arr:[YourObject] = []
    then add a variable to YourObject ex: var isSelected: Bool = false

    in the cellForRow method, check your current item selection status and change the cell selection style.

    and finally, in didSelectRow method, change your current selected object isSelected variable status, then reload table view

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search