Search code examples
uitableviewswiftsubtitledetailtextlabel

How do I show a hidden subtitle in a UITableView when selected?


Is there a way to hide the subtitles of all the cells until you select a cell - then it only shows you that cell's subtitle? I tried the following code - which successfully hides all the subtitles but fails to show one when I select a cell:

if cell.selected {
        cell.detailTextLabel?.hidden = false
    } else {
        cell.detailTextLabel?.hidden = true
    }

Thanks for any help.

Edit 2 - I ended up doing this in my didSelectRowAtIndexPath:

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

    for cell in tableView.visibleCells() {
        cell.detailTextLabel??.hidden = true
    }
    var cell = tableView.cellForRowAtIndexPath(indexPath)
    cell?.detailTextLabel?.hidden = false

}

Thanks very much, Christian!


Solution

  • Just use the didSelectRowAtIndexPath method and access the touched cell. Then you can show the detailTextLabel.

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID)  as UITableViewCell
    
        cell.detailTextLabel?.hidden = false
    }