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

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:
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.)

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

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
}

You can do this a couple ways...
tableView.allowsSelection = false
You can set the tableView in xCode Storyboard to not have any selection under the fourth tab.
Or, you can do this on the cell cell.selectionStyle = UITableViewCellSelectionStyle.None
What you want is ultimately going to be about what behavior you are going after. Just do a little experimenting.

Swift 3
In a custom cell add this:
override func awakeFromNib() {
super.awakeFromNib()
selectionStyle = .none
}
This ensures you won't even see the gray when the cell is tapped. This code in the UITableViewDelegate only deselects when tapped.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}

Just Simply click on the cell and go to attributes inspector you will find Selection Style , select none.

You can change style by:
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];

Swift 4.1
tableView.deselectRow(at: indexPath, animated: true)

In the storyboard or xib,
In the Tableview cell,
you can select selection to "none",
in the atributes inspector

Related

How to deselect a UITableView Cell when another one is selected?

I want to deselected a table view cell when another cell is selected. Here is the code that's just deselect the cell for second tap:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
You don't need to call tableView.deselectRow(), just simply set multipleSelection to false.
With Storyboard
Programmatically
tableView.allowsMultipleSelection = false
Select tableView & go to Attributes Inspector & set selection to single selection.
Programatically (inside viewDidLoad() ) you can set as:
self.tableView.allowsSelection = true
self.tableView.allowsMultipleSelection = false

TableView Cell not loading View Controller until Different TableView Cell is clicked

I've searched around and haven't truly found why this is happening. Basically, I followed this tutorial https://www.youtube.com/watch?v=yupIw9FXUso by Jared Davison on creating Table View Cells to Multiple View controllers. In his example, everything works perfectly, but for some reason when you click on a table view cell in my code the cell is highlighted in grey. Then, when the user clicks on a separate table view cell the view controller that should have been loaded by the first table view cell is loaded. If the user then clicks back on the original table view cell the page that should have been loaded by the second table view cell is loaded. In summary, all of the view controllers are loaded a "click" behind.
Here is the code for the table view:
//Feed Navigation Functions
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elements.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 75
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "feedCell") as! FeedTableViewCell
cell.txtTitle.text = "The Fight Against \(elements[indexPath.row])"
cell.issueIcon.image = UIImage(named: "Issue Icons_\(elements[indexPath.row])")
return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
Update: The array is a simple array of strings for example [One, Two, Three] there are 6 strings in the array.
When you select a cell then select another one the method didDeselectRow is called so the Vc is pushed you actually want to implement didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
// this to deSelect it after push
tableView.deselectRow(at: indexPath, animated: false)
}
This method fired when a cell is selected
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath
This method fired when a cell is deSelected
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath
You want to use the tableView's delegate method didSelectRow instead of didDeselectRow I think...
Issue: click on a table view cell in my code the cell is highlighted in grey
This is due to the selectionStyle, which you can read about here. If you don't want the cell highlighted, you can set cell.selectionStyle = .none.
Edit: As indicated in other correct response - issue was with incorrect/typo in method - we should use didSelectRowAt not didDeselectRowAt.

iOS UITableViewCellEditingStyle set check (ON) programatically

When I click on Edit button in my screen i change my TableView to edit mode and set the edit style as check box by doing this
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.init(rawValue: 3)!
}
now I need programatically pre check some of the entries.
How can i Make some of the cells as checked?
I know in
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
I have to set the edit style which marks it as checked.
I finally figured it out.
Setting the Checkbox in an table view is equivalent to selecting the cell.
so all you need to do is mark the sell as selected, nothing to do with the Edit style
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell
let item = self.items[indexPath.row]
cell.setup(item: item)
cell.setSelected(true, animated: true) // Provided your cell is already in check box edit mode, then this makes it CHECK ON
return cell
}
By the way. I saw an explanation in similar question here (on SO) that you should not use this:
return UITableViewCellEditingStyle.init(rawValue: 3)!
Because it's not public API and if Apple changes this value, application will stop working properly. The same behaviour can be reached simply by enabling Multiple selection during editing:
You can check it easily by setting breakpoint on your overridden method
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.init(rawValue: 3)!
}
And enable editing during running the app with Multiple selection during editing and without it. You will see that if Multiple selection during editing is enabled, this method will not be called at all.

How to disable UITableViewCell highlighting?

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?
Set UITableViewCell selection style none
cell.selectionStyle = .None
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)
UITableViewDelegate has methods to deal with cell highlights, probably
tableView(_:shouldHighlightRowAt:) is what you are looking for
Check the documentation for the other methods
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
}

In my UITableView, how can I reset the "selected cell color" back to default upon selection?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
checkRequirements() { () -> Void in
var room = self.rooms[indexPath.row]
self.roomForPreview = room
self.indexPathForPreview = indexPath
self.performSegueWithIdentifier(CONSTANTS.SegueLobbyToRoomPreview, sender: self.view)
}
}
I perform a segue when a user selects a cell, but when I unwind, I find that the cell still has a grey background.
How can I make the cell grey only on touch? Once the touch is gone, the cell goes back to normal color.
Use deselectRowAtIndexPath:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
...
If you're using a UITableViewController, you can set the clearsSelectionOnViewWillAppear property to true - this will work as well.

Resources