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
I have a likeness problem. In your cellForRowAtIndexPath method set:
cell.selectionStyle = .None
and then set didHighlightRowAtIndexPath...
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell!.contentView.backgroundColor = .green
}
func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell!.contentView.backgroundColor = .clear
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
updateUI(isSelected: selected)
}
private func updateUI(isSelected: Bool){
label.textColor = isSelected ? .red : .green
//etc..
}
You've the right method already in there: didSelectRowAtIndexPath. In that method you can call tableView.cellForRowAtIndexPath(indexPath) and get your cell. Than you can set the cell-background to your color:
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("Row \(indexPath.row) selected")
let cell:YourCustomCell = tableView.cellForRowAtIndexPath(indexPath) as YourCell
cell.backgroundColor = UIColor.redColor()
}
Or, a better way would be to check in your cellForRowAtIndexPath method, if a cell is selected:
if(cell.selected){
cell.backgroundColor = UIColor.redColor()
}else{
cell.backgroundColor = UIColor.clearColor()
}
override func awakeFromNib() {
super.awakeFromNib()
selectedBackgroundView = UIView()
selectedBackgroundView?.backgroundColor = .blue
}
Why other approaches are wrong:
setSelected method, because iOS handles the animation for us.backgroundColor also doesn't behave the same as iOS does itNone of above answers worked, so I try mine and it worked: Here it is:-
func reloadcell() {
if isSelected {
conView.backgroundColor = .yellow
} else if isHighlighted {
conView.backgroundColor = .yellow
} else {
conView.backgroundColor = .clear
}
}
and call that function in layoutsubviews and in your didselect method
To keep your code clean you should think about moving screen design related code for your cells from UITableViewController into a UITableViewCell class.
Your UITableViewController` needs only to set the selected state of the cell as follows:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
guard let cell = tableView.cellForRow(at: indexPath) else { return }
cell.setSelected(true, animated: true)
}
Your desired customisation can be implemented in a derrived UITableViewCell class by overriding var isSelected. With this solution you could have even different select colors for each cell.
class MyTableViewCell: UITableViewCell
{
@IBOutlet weak var label:UILabel!
override var isSelected: Bool
{
didSet{
if (isSelected)
{
self.backgroundColor = UIColor.red
if let label = label
{
label.textColor = UIColor.white
}
}
else
{
self.backgroundColor = UIColor.white
if let label = label
{
label.textColor = UIColor.black
}
}
}
}
}