user3746428
user3746428

Reputation: 11175

Hide label when UITableView Cell is tapped

I have a label within my cells that I would like to show/hide when tapped. I know that I have to get the index path of the cell that was tapped from within didSelectRowAtIndexPath. But I am unsure of how I then show/hide the label in that particular cell.

Am I able to show/hide it from within didSelectRowAtIndexPath, or is there a way to deal with it in cellForRowAtIndexPath and then update it?

I did some research into this but I really couldn't find a whole lot.

Here is all I have so far:

var selectedRowIndex: NSIndexPath = NSIndexPath(forRow: -1, inSection: 0)

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedRowIndex = indexPath
}

Upvotes: 3

Views: 6262

Answers (1)

Mundi
Mundi

Reputation: 80265

Here is how you get to the cell from the index path:

let cell = tableView.cellForRowAtIndexPath(indexPath) as MyCustomCell
cell.myTextLabel.hidden = true

Also, depending on your needs, you might want to deselect the cell as well.

tableView.deselectRowAtIndexPath(indexPath)

Upvotes: 11

Related Questions