Unselect cell in tableview if the cell is visible

In a tableview, the multiple selection is set in the storyboard. The following code is used to unselect a cell:

 tableView.deselectRow(at: indexPath, animated: true)

It works fine if the cell is not visible. If the cell is visible, the selection check mark is gone. However, the cell seems still to be in selection mode, and it cannot be selected again.

Here is more information for my case: I have a tabview with 2 tabs. One is for editing data, and the other is for displaying data in a tableview.

n the second tabview, I made selections of cells. Then switch to the first tabview of the data source. If there is any change in the data source, I'll set a flag about the change. In the second tabview, I'll clear any selection if the flag is on.

override open func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated)
  guard isDatasourceChanged else { return }

  let selRows =
  tableView.indexPathsForVisibleRows ?? []
  for ip in selRows
  {
    tableView.deselectRow(at: ip, animated: true)
  }
}

For example, here is the cell is selected:

I changed the datasource tabview first, and then back to the second tabview. The viewWillAppear event is called and all selections are set to deselected. The visible cell looks like this: the selection check mark is gone, but it looks like it is still highlighted and cannot be selected again.

I verified that the hi lighted cell is not in selected cell, but I cannot select the cell again.

How can I set the cell unselected and not hilighed if it is visible?

Answered by chudq in 726200022

Finally, I found a solution to resolve the issue. I think this may be a bug in UIKit. At least in my case, the visible cells can be unselected, but the unselection programmatically does present an issue.

I wrote a blog to discuss the issue in detail, and I put my solution at the end.

https://davidchuprogramming.blogspot.com/2022/09/unselect-cell-in-tableview-if-cell-is.html

Accepted Answer

Finally, I found a solution to resolve the issue. I think this may be a bug in UIKit. At least in my case, the visible cells can be unselected, but the unselection programmatically does present an issue.

I wrote a blog to discuss the issue in detail, and I put my solution at the end.

https://davidchuprogramming.blogspot.com/2022/09/unselect-cell-in-tableview-if-cell-is.html

Unselect cell in tableview if the cell is visible
 
 
Q