1

I have selected some tableView cells and stored it in UserDefaults, also showed what i have selected. Now after adding a clear button and made deselect all selected row & emptied the selected row array followed by table reloadAll(). It is not displaying dynamically? How to clear selections from a button action and show tableView without selections immediately?

  @IBAction func clearSelection(_ sender: Any) {

//        self.sellerTableView.reloadInputViews()
//
//        self.sellerTableView.deselectRow(at: , animated: true)

This one is not working!

        if let aRow = self.sellerTableView.indexPathForSelectedRow {
            self.sellerTableView.deselectRow(at: aRow, animated: true)
        }
//        self.userSelectedSellers.removeAll()

        self.sellerTableView.reloadData()
    }
Niranjan
  • 13
  • 7

2 Answers2

1

Here is the way to deselect all cells from a tableView :

for cell in tableView.visibleCells {
    cell.setSelected(false, animated: true)
}

tableView.reloadData()
marc
  • 914
  • 6
  • 18
0

As you described above that you have a tableview with multiple selection, you should first create a array which adds your selected indexPaths and then you write -

if indexArray.contains(indexPath.row) {
*// then set the table cell as selected*
}
else {
*// remove the table view selection*
}

You can do the table cell select and deselect as your requirement. like changing the color or place a tick mark .... any think ..

then you do something like this in the didSelect Delegate -

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

if indexArray.contains(indexPath.row) {
        if let indexForRemove = indexArray.index(of: indexPath.row) {
            indexArray.remove(at: iindexForRemove)
        }
    }
    else {
        indexArray.append(indexPath.row)
    }

// Now reload your table view--- Done

Rahul Singha Roy
  • 548
  • 1
  • 3
  • 13