Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do my UITableViewCells turn grey when I tap on them?

Tags:

When I tap on the cells of my table view, they darken to a grey color, and don't turn back to white until I tap on a different cell. Is there some sort of Boolean I have to set for it to not do that?

Here's a screenshot explaining my problem:

enter image description here

Links to other websites would be helpful, if it would mean a more detailed description. (Unless it's a super simple fix, then the right code or steps-to-take would be easier than a link.)

like image 716
Christian Kreiter Avatar asked Oct 09 '15 20:10

Christian Kreiter


2 Answers

This is the default behaviour of UITableView.

You must call deselectRowAtIndexPath inside of didSelectRowAtIndexPath inside your UITableViewController class.

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

Check out the iOS Documentation for more information.

UITableView
UITableViewDelegate

like image 91
Qasim Asghar Avatar answered Oct 05 '22 09:10

Qasim Asghar


Swift 3

Option 1: (Which I always use)

To give it fade out animation after selected with gray you can do this:

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

Option 2:

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {     let cell = .....     cell.selectionStyle = .none     return cell } 
like image 45
MBH Avatar answered Oct 05 '22 10:10

MBH