Search code examples
iosswiftuitableviewhighlightvisual-effects

Make UITableViewCell flash briefly


I have a UITableView set-up and I am exploring all the different options of highlighting the UITableViewCell and I was wondering the following:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        .
        .
    cell.selectionStyle = .gray
    return cell
}

What happens now is that when a cell is selected it is highlighted with a grey color, I was wondering, is it possible to deselect the cell after about 0.5 sec?

Basically is it possible to make the cell only flash briefly so the user knows he interacted with the cell, however it does not stay selected? I am using this tableView for a side menu and staying highlighted is not the desired behavior for me.

Thanks!


Solution

  • You could deselect the row on the didSelectRow method, the cell will be highlighted and then the selection will fade out, letting the user know that they interacted with the cell.

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      tableView.deselectRow(at: indexPath, animated: true)
      // Do whatever else you need
    }