KingTim
KingTim

Reputation: 1301

(Swift) Tableview cell: textLabel showing up, but detailTextLabel isn't

I set up a table view in storyboard and gave it a custom cell class:

Storyboard:

enter image description here

Cell class:

class CommentCell: UITableViewCell {

override func layoutSubviews() {
    super.layoutSubviews()
    textLabel?.frame = CGRect(x: 100, y: textLabel!.frame.origin.y - 2, width: textLabel!.frame.width, height: textLabel!.frame.height)
    detailTextLabel?.frame = CGRect(x: 100, y: detailTextLabel!.frame.origin.y + 2, width: detailTextLabel!.frame.width, height: detailTextLabel!.frame.height)
}

let logoView: UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFit
    imageView.layer.borderWidth = 1
    imageView.layer.cornerRadius = 24
    imageView.layer.masksToBounds = true
    return imageView
}()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)

    addSubview(logoView)
    logoView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8).isActive = true
    logoView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    logoView.widthAnchor.constraint(equalToConstant: 48).isActive = true
    logoView.heightAnchor.constraint(equalToConstant: 48).isActive = true
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

}

And then in my view controller I gave it some dummy text to test it out, and the textLabel text shows up fine, but the detailTextLabel text and the imageView do not.

@IBOutlet weak var tableView: UITableView!
let commentors = ["Person One", "Person Two", "Person Three"]
let comments = ["Comment One", "Comment Two", "Comment Three"]


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CommentCell

    cell.textLabel?.text = commentors[indexPath.row]
    cell.detailTextLabel?.text = comments[indexPath.row]
    DispatchQueue.main.async {
        cell.logoView.image = UIImage(named: "testImage")
    }

    return cell
}

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

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 72
}

This is what I see when I run the app:

enter image description here

Anyone know why the textLabel is showing up but not the detailTextLabel or the image?

Upvotes: 0

Views: 624

Answers (2)

regina_fallangi
regina_fallangi

Reputation: 2198

@vadian's answer is correct, however, you do not have to add the other label manually. I was facing the same issue and realised while looking at your question that the type of the cell in the Interface Builder is Custom and not subtitle. You initialise it correctly, but it seems like the style set in the IB has priority. subtitle style makes sure that the detailTextLabel shows up and shows it already in the storyboard.

Hope this helps others.

Upvotes: 0

vadian
vadian

Reputation: 285069

A table view cell with style custom has no implicit detailTextLabel and no imageView.

You have to implement the elements in the storyboard and connect them to appropriate IBOutlets.

Upvotes: 1

Related Questions