I am trying to change the appearance of a custom selected TableViewCell using Swift.
Do I need to do it via the designer or programmatically?
I tried the fol
My two cents: the proper way of doing it (also visually) is to use the designated view in a (tableView)cell, that is the selectedBackgroundView property. However, you need to initialize it first with UIView()
SWIFT 3.0
override func awakeFromNib() {
super.awakeFromNib()
self.selectedBackgroundView = UIView()
self.selectionStyle = .default // you can also take this line out
}
Then you can use it in your customized cell as follows:
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.selectedBackgroundView!.backgroundColor = selected ? .red : nil
}
That's it. Of course you can also integrate the above in your UITableView functions referred to above. Check it out.
SWIFT 5 Update
Set the selection style to .none in the cellForRowAT
method:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell
cell.selectionStyle = .none
return cell
}
Then implement the didHighlightRowAt
and the didUnhighlightRowAt
methods:
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell!.contentView.backgroundColor = .red
}
func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
// Add timer to be able see the effect
Timer.scheduledTimer(withTimeInterval: 0.2, repeats: false) { (_) in
cell!.contentView.backgroundColor = .white
}
}
Update for Swift 3
This answer is based on Cao Yong answer, and it is intended as an update for Swift 3
For Swift 3, use the following code in your cellForRowAt indexPath method set:
cell.selectionStyle = .none
Then, set it in didHighlightRowAtIndexPath
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell!.contentView.backgroundColor = .red
}
func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell!.contentView.backgroundColor = .clear
}
When you tap a cell, a subviews background color is actually being changed. That subview is 'selectedBackgroundView'. You can override the view of each cell in the cellForRowAtIndexPath TableView delegate method.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("identifier", forIndexPath: indexPath)
let selectedView = UIView()
selectedView.backgroundColor = UIColor(red: 250/255, green: 250/255, blue: 250/255, alpha: 1.0)
cell.selectedBackgroundView = selectedView
return cell
}
Change the color to whatever you like.
First Call This method-
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "Show Label"
cell.backgroundColor = UIColor.redColor()
}
And than call this method
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.backgroundColor = UIColor.clearColor()
}
For CollectionView==
1-
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = dateCollectionView.cellForItemAtIndexPath(indexPath) as! DateCollectionViewCell
cell!.dateLabel.backgroundColor = UIColor.redColor()
}
2-
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
let cell = dateCollectionView.cellForItemAtIndexPath(indexPath) as? DateCollectionViewCell
cell!.dateLabel.backgroundColor = UIColor.clearColor()
}