11

I have a dark gray view background with a transparent tableview. I'm using the following code to try and stop cell highlight when a cell is clicked. It works except right when the cell is initially clicked, I see a highlight. I then transition to another scene after that. When I come back, the cell is not highlighted.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    selectedCell.contentView.backgroundColor = UIColor.clearColor()
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

How do I disable the initial cell highlighting that is still going on?

4thSpace
  • 43,672
  • 97
  • 296
  • 475
  • 1
    Set `cell.selectionStyle = UITableViewCellSelectionStyleNone;` follow this link: [link](http://stackoverflow.com/questions/11920156/custom-uitableviewcell-selection-style) – Chandan Jul 20 '16 at 18:55

4 Answers4

17

Set UITableViewCell selection style none

cell.selectionStyle = .None
Sameer Subba
  • 196
  • 3
5

UITableViewDelegate has methods to deal with cell highlights, probably tableView(_:shouldHighlightRowAt:) is what you are looking for

Check the documentation for the other methods

Leonardo
  • 1,740
  • 18
  • 27
5

I found that the other answers did not work for me as they required a double click to select. This did work.

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

tableView.deselectRow(at: indexPath, animated: true)
markhorrocks
  • 1,199
  • 19
  • 82
  • 151
  • This question requests that the highlight not appear when clicked. This answer does not prevent that visual effect, and it accentuates the visual impact when the selection -> unselected, because it used `animated: true` – benc Jul 26 '23 at 15:32
3

The simplest way to prevent highlighting is setting selectionStyle to None. Here's how you can achieve this :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    cell.selectionStyle = .None
    return cell
}
AnthoPak
  • 4,191
  • 3
  • 23
  • 41
  • NOTE: this is a fast and easy workaround with stock table/cell config, because it is just mapping the highlighted's visual state to the (default) unselected visual state. If you want to truly disable highlighting's events, see answer -> https://stackoverflow.com/a/38488665/2910 – benc Jul 26 '23 at 15:38