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.
6 Answers
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)
}
-
1Why a question mark after
tableView
? It is of typeUITableView
notUITableView?
. Commented May 23, 2016 at 5:24
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
}
}
-
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. Commented Feb 19, 2015 at 9:12
-
This will actually just disable the highlighting - selecting would still work.– ZorayrCommented Feb 19, 2015 at 21:11
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)
}
}
-
-
1I think you should do this in
viewWillAppear
, otherwise the deselection animation begins after the view controller appears and seems a little late.– LucaCommented Jan 4, 2019 at 4: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.– AzamCommented Nov 19, 2019 at 3:16
Solution for Swift3 :
It will allow selection and this is the fix for swift 3.
cell.selectionStyle = UITableViewCellSelectionStyle.none
self.selectionStyle = UITableViewCellSelectionStyle.None
Should suffice in override func awakeFromNib() too :)
I got a fatal error when adopting the above code.
-
This is fine, but it does not show anything to the user about the cell that he/she has selected Commented Sep 7, 2016 at 7:13
Swift 3
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
-
why answered the question which already same as yours? If you have better solution, you are welcomed to edit. Commented Oct 15, 2017 at 13:50