Why does my UITableViewCells turn gray when I click on them?

When I click on cells in my table view, they darken to gray and do not turn white until I click on another cell. Is there some kind of boolean type that I have to set so that it doesn't do this?

Here is a screenshot explaining my problem:

enter image description here

Links to other sites will be useful if it means a more detailed description. (If this is not a very simple fix, then the correct code or step-by-step instructions will be simpler than the link.)

+9
source share
6 answers

This is the default behavior for a UITableView.

You must call deselectRowAtIndexPath inside didSelectRowAtIndexPath inside your UITableViewController class.

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) } 

See the iOS documentation for more information.

UITableView
UITableViewDelegate

+25
source

Swift 3

Option 1: (which I always use)

To cancel the animation after grayscale, you can do this:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) } 

Option 2:

To completely remove the highlight effect, you can add this line to your cellForRowAt :

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = ..... cell.selectionStyle = .none return cell } 
+9
source

You can do this in several ways ...

  • tableView.allowsSelection = false

  • You can set tableView in xCode Storyboard so that there is no choice under the fourth tab.

  • Or you can do it in cell.selectionStyle = UITableViewCellSelectionStyle.None

What you want will ultimately be related to what kind of behavior you will continue. Just experiment a bit.

+7
source

Swift 3

In the custom cell, add the following:

  override func awakeFromNib() { super.awakeFromNib() selectionStyle = .none } 

This ensures that you do not see the gray color when the cell is pressed. This code in UITableViewDelegate is canceled only when clicked.

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) } 
+2
source

You can change the style:

  [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 
+1
source

Swift 4.1

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

Source: https://habr.com/ru/post/1233381/


All Articles