13

I am having problem with my table view cell. I can't disable the highlight of table cell selection after returning from detail view to main table view using segue with embedded navigation controller. The table cell is still selected. I don't want the table cell selection to be disable when I click one of them to show the detail. I only want to disable them after I returning back from detail view.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Thiha Aung
  • 5,036
  • 8
  • 36
  • 79

6 Answers6

41

I got it now. I solved like this. It's simple theory.

We just deselect it when we select the row

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Thiha Aung
  • 5,036
  • 8
  • 36
  • 79
4

You could also disable cell selection highlighting:

import Foundation

class CustomTableViewCell: UITableViewCell
{
  required init(coder aDecoder: NSCoder)
  {
    fatalError("init(coder:) has not been implemented")
  }

  override init(style: UITableViewCellStyle, reuseIdentifier: String?)
  {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    self.selectionStyle = UITableViewCellSelectionStyle.None
  } 
}
Zorayr
  • 23,770
  • 8
  • 136
  • 129
  • Thank you sir.I really appreciate your help.I can't disable my table cell selection.i only want to hide the highlight after i select one of the cell and coming back from detail. – Thiha Aung Feb 19 '15 at 09:12
  • This will actually just disable the highlighting - selecting would still work. – Zorayr Feb 19 '15 at 21:11
4

The way I like to do it, if you have single selection enabled is to use the viewDidAppear Method. That way the user can see the deselection animation when he returns to the tableView.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    if let selectedRow = tableView.indexPathForSelectedRow {
        tableView.deselectRow(at: selectedRow, animated: true)
    }
}
Kirill
  • 738
  • 10
  • 26
André Kuhlmann
  • 4,378
  • 3
  • 23
  • 42
  • Thanks for cooperating to the answer – Thiha Aung Oct 15 '17 at 13:48
  • 1
    I think you should do this in `viewWillAppear`, otherwise the deselection animation begins after the view controller appears and seems a little late. – Luca Jan 04 '19 at 04:27
  • This solution is similar to Notes app. For me, this looks more elegant compared to making highlight disappear right after user tap the cell. – Azam Nov 19 '19 at 03:16
3

Solution for Swift3 :

It will allow selection and this is the fix for swift 3.

cell.selectionStyle = UITableViewCellSelectionStyle.none
MANISH PATHAK
  • 2,602
  • 4
  • 27
  • 31
1
self.selectionStyle = UITableViewCellSelectionStyle.None

Should suffice in override func awakeFromNib() too :)

I got a fatal error when adopting the above code.

David West
  • 1,550
  • 1
  • 18
  • 31
0

Swift 3

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
markhorrocks
  • 1,199
  • 19
  • 82
  • 151
  • why answered the question which already same as yours? If you have better solution, you are welcomed to edit. – Thiha Aung Oct 15 '17 at 13:50