Search code examples
uitableviewswiftdetailtextlabel

UITableViewCell not showing detailTextLabel.text - Swift


The detail (subtitle) text does not appear. The data are available, though, because when a println() call is added, it prints Optional("data") to the console with the expected data. In the storyboard, the UITableViewController is set to the proper class, the Table View Cell Style is set to 'Subtitle', and the reuse identifier is set to 'cell'. How can I get the subtitle information to display?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    dispatch_async(dispatch_get_main_queue(), { () -> Void in

        cell.textLabel.text = self.myArray[indexPath.row]["title"] as? String
        cell.detailTextLabel?.text = self.myArray[indexPath.row]["subtitle"] as? String

        println(self.myArray[indexPath.row]["subtitle"] as? String)
        // The expected data appear in the console, but not in the iOS simulator's table view cell.

    })
    return cell
}

Solution

  • Same issue here (from what I've read, perhaps a bug in iOS 8?), this is how we worked around it:

    1. Delete the prototype cell from your storyboard

    2. Remove this line:

    var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    1. Replace with these lines of code:
    let cellIdentifier = "Cell"
                
    var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: cellIdentifier)
    }
    

    Update for Swift 3.1

    let cellIdentifier = "Cell"
        
    var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.value2, reuseIdentifier: cellIdentifier)
    }
    

    Update for Swift 4.2 - Simplified

    let cell = UITableViewCell(style: UITableViewCell.CellStyle.value2, reuseIdentifier: "cellId")
    

    Update for Swift 5 - Simplified

    let cell = UITableViewCell(style: .value2, reuseIdentifier: "cellId")