skip to Main Content

I’m smashing my head against the wall for the past 3 hours trying to figure this out. After watching countless tutorials, I still can’t seem to figure out why this XIB cell cannot show both labels on the tableview.

XIB file:
enter image description here

What it keeps showing up as:

enter image description here

I just don’t understand. I’ve tried everything, setting height constraints, distance constraints, stack views, but nothing will get the second label on the bottom to show up in the table view cell. Is there an obvious thing I am missing here?

2

Answers


  1. Chosen as BEST ANSWER

    OK, this is absolutely insipid but I figured it out, by randomly copying and pasting bits of code online until something worked. Here's what worked (swiped from hackingwithswift)

        override func awakeFromNib() {
        super.awakeFromNib()
        NSLayoutConstraint.activate([
            titleLabel.topAnchor.constraint(equalTo: contentView.topAnchor,constant: 5),
            titleLabel.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
            titleLabel.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor),
            titleLabel.bottomAnchor.constraint(equalTo: projLabel.topAnchor),
            
            
            projLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor),
            projLabel.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor),
            projLabel.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor),
            projLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
        ])
    

    final result: enter image description here


  2. Try this:

    Select TableView in Storyboard -> Identity Inspector

    • Set Row Height to Automatic (let empty)
    • Set estimate to some value (for example 150)

    On cell class implement this method (example):

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        // 1) Set the contentView's width to the specified size parameter
        contentView.pin.width(size.width)
    
        // 2) Layout the contentView's controls
        layout()
    
        // 3) Returns a size that contains all controls
        return CGSize(width: contentView.frame.width, height: adsViewsCount.frame.maxY + padding)
    }
    

    Implement this with UITableViewDatasource:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        // The UITableView will call the cell's sizeThatFit() method to compute the height.
        // WANRING: You must also set the UITableView.estimatedRowHeight for this to work.
        return UITableView.automaticDimension
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search