10

I have a Custom Tableview cell in swift and in that cell a label.

I want to be able to change the label when you select a cell.

How can I reference my custom UITableviewCell label in didSelectRowAtIndexPath

In Objective C to reference my custom cell in didSelectRowAtIndexPath I would use the following:

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

What must I do in swift to achieve the same result?

milo526
  • 5,012
  • 5
  • 41
  • 60
user3110353
  • 512
  • 2
  • 7
  • 17

6 Answers6

12

You just need to translate the same code to Swift.

var myCell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
myCell.customLabel.textColor = UIColor.redColor()
Shamas S
  • 7,507
  • 10
  • 46
  • 58
  • by this solution, if I clicked on many cells, all cells will be red ? or only the selected one will be red, and the others will back to the essential color? – Rawan Jan 28 '16 at 08:56
  • I think all selected cells will be red, but if you are reusing cells, inconsistent behaviour will appear. – Shamas S Jan 28 '16 at 09:07
  • thanks :) but do you now how to make the other cells take the essential color? Really all the selected cells are red, and didn't back to the essential color after select other cells :S – Rawan Jan 28 '16 at 09:11
  • If you are playing between 2 cells only, then you can keep `indexPath` as a class variable. Each time a cell is selected, you get previous cell with savedIndexPath, unselect its color, get newer cell and select its color and save the new indexPath. – Shamas S Jan 28 '16 at 09:15
  • no not 2 cells @_@ i'm working on tableview with number of cell unknown for me :S – Rawan Jan 28 '16 at 09:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101859/discussion-between-shamas-s-and-rawan). – Shamas S Jan 28 '16 at 09:31
8

The Above Answers Are Incomplete

Because a UITableView reuses cells you need to check if the cells are selected and adjust the color appropriately in cellForRowAtIndexPath. There may be typos, but this is the complete 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)

}
WaterNotWords
  • 997
  • 1
  • 9
  • 24
  • 1
    i tried this code for a collection view; when i select a cell, it gets selected and then deselected automatically !! so i can see the label color changing twice ! – Dory Daniel Mar 31 '17 at 12:46
  • You'll have to post you code if you want help. The code in this answer doesn't deselect the cell. If you ask a new question someone will help you. If you message me or link to it in a comment here, I will try to help. – WaterNotWords Apr 06 '17 at 03:42
4

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 // for text color
    Cell?.backgroundColor = UIColor.red // for cell background color
}
Ahmed Safadi
  • 4,402
  • 37
  • 33
3

why not use setSelected in UITableViewCell subclass?

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 go back to deselected color after a specific amount of 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 selectionStyle = .none

JustinM
  • 2,202
  • 2
  • 14
  • 24
2

Try this,

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    var Cell = tableView.cellForRowAtIndexPath(indexPath) as! MPSurveyTableViewCell
  Cell. customLabel.textColor = UIColor. redColor()
}
DHEERAJ
  • 1,478
  • 12
  • 32
0
let CellObject = tableView.dequeueReusableCell(withIdentifier: "your cell identifier name") as! MPSurveyTableViewCell
CellObject.customLabel.textColor = UIColor.red
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Prasann
  • 135
  • 1
  • 9