UITableViewCell not showing detailTextLabel.text - Swift

十年热恋 提交于 2019-11-30 02:53:13

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

Your code looks fine. Just goto the storyboard and select the cell of your tableview -> Now goto Attributes Inspector and choose the style to Subtitle.

Follow this according to the below screenshot.

Hope it helped..

If you still want to use prototype cell from your storyboard, select the TableViewcell style as Subtitle. it will work.

If doing so programmatically without cells in interface builder this code works like a charm in Swift 2.0+

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    yourTableView.delegate = self
    yourTableView.dataSource = self
    yourTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourTableViewArray.count
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell: UITableViewCell = yourTableView.dequeueReusableCellWithIdentifier("subtitleCell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = "the text you want on main title"
    cell.detailTextLabel?.text = "the text you want on subtitle"

    return cell
}
Hugh Jeffner

If you are setting the text to nil somewhere when you try to set it to a non-nil value the actual view that contains the text will be missing. This was introduced in iOS8. Try setting to an empty space @" " character instead.

See this: Subtitles of UITableViewCell won't update

Try this it work for me (swift 5)

let cell = UITableViewCell(style: .value1, reuseIdentifier: "cellId")
cell.textLabel.text = "Déconnexion"
cell.imageView.image = UIImage(named: "imageName")

Objective c :

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellId"];

For what it's worth: I had the problem of detail not appearing. That was because I had registered the tableView cell, which I should not have done as the cell prototype was defined directly in storyboard.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!