Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

uitableview - 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
}
question from:https://stackoverflow.com/questions/27221003/uitableviewcell-not-showing-detailtextlabel-text-swift

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...