Swift 4 UITableView Cell height not working

Viewed 4451

I'm trying to set the height on Xcode9, except it doesn't seem to work. I can get away with a fixed size of 76. I have an Image in there which I want to have 10 padding with a height of 56.

What I've tried: 1. Setting it in the Size Inspector 2. Through coding, but it's not working

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 76
    getArticles() // returns json data
}

And the tableView

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "ArticleCell") as? ArticleCell else { return UITableViewCell() }

    cell.titleLabel.text = articles[indexPath.row].title

    if let imageURL = URL(string: articles[indexPath.row].image) {
        DispatchQueue.global().async {
            let data = try? Data(contentsOf: imageURL)
            if let data = data {
                let image = UIImage(data: data)
                DispatchQueue.main.async {
                    cell.imgView.image = image
                }
            }
        }
    }

    cell.contentView.setNeedsLayout()
    cell.contentView.layoutIfNeeded()

    return (cell)
}

Nothing seems to work, and it stays at the same height. I have tried it with and without contraints.

What am I doing wrong here?

EDIT I have changed from Prototype cell to Xib. Now I have it working, except of I get constraints errors.

The constraints setup This is the only way it works. If I remove the bottom margins, it goes back to the smaller size again. Same if I remove the image height.

    2017-09-25 19:26:37.842902+0200 Proj010916-NL_nl[56361:915542] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x60c000093c90 UIImageView:0x7fec2800cd60.height == 56   (active)>",
    "<NSLayoutConstraint:0x60c000093ce0 UIImageView:0x7fec2800cd60.top == UITableViewCellContentView:0x7fec2800aa70.topMargin + 10   (active)>",
    "<NSLayoutConstraint:0x60c000093d80 UITableViewCellContentView:0x7fec2800aa70.bottomMargin == UIImageView:0x7fec2800cd60.bottom + 10   (active)>",
    "<NSLayoutConstraint:0x604000097160 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fec2800aa70.height == 92   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60c000093c90 UIImageView:0x7fec2800cd60.height == 56   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

Next task is to redo the segue since it's now a vc and not a cell inside anymore.

1 Answers

Have you tried implementing the heightForRowAtIndexPath delegate method?

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

That way you can set different heights for each indexPath in the UITableView.

Related