Swift Change the color of the custom label for the table label when you select

I have a custom Tableview cell in swift, and a label in that cell.

I want to change the label when selecting a cell.

How can I reference my custom UITableviewCell label in didSelectRowAtIndexPath

In Objective-C, I would use the following to refer to my custom cell in didSelectRowAtIndexPath :

 MPSurveyTableViewCell *cell = (MPSurveyTableViewCell *)[tableViewcellForRowAtIndexPath:indexPath]; cell.customLabel.TextColor = [UIColor redColor]; 

What do I need to do quickly to achieve the same result?

+8
ios uitableview swift didselectrowatindexpath custom-cell
source share
5 answers

You just need to translate the same code into Swift.

 var myCell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell myCell.customLabel.textColor = UIColor.redColor() 
+10
source share

Above answers are not filled

Since UITableView reuses cells, you need to check if the cells are selected and adjust the color accordingly in cellForRowAtIndexPath. There may be typos, but this is the full answer:

 func tableView(tableView: UICollectionView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifierHere", forIndexPath: indexPath) as! MPSurveyTableViewCell // setup your cell normally // then adjust the color for cells since they will be reused if cell.selected { cell.customLabel.textColor = UIColor.redColor() } else { // change color back to whatever it was cell.customLabel.textColor = UIColor.blackColor() } return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ let cell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell cell.customLabel.textColor = UIColor.redColor() tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None) } func tableView(tableView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell // change color back to whatever it was cell.customLabel.textColor = UIColor.blackColor() tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None) } 
+3
source share

Try it,

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) var Cell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell Cell. customLabel.textColor = UIColor. redColor() } 
+2
source share

swift 3 xcode 8

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) let Cell = tableView.cellForRow(at: indexPath) Cell?.textLabel?.textColor = UIColor.red } 
+2
source share

Why not use setSelected in a subclass of UITableViewCell?

 override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) customLabel.textColor = selected ? UIColor.red : UIColor.black } 

or if you want it to return to the highlighted color after a certain time:

 override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) if selected { customLabel.textColor = UIColor.red DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2, execute: { [weak self] in self?.customLabel.textColor = UIColor.black } } } 

Then just set your cell selection Style = .none

+2
source share

All Articles