pableiros
pableiros

Reputation: 16032

Self sizing cells with three UILabels not working

This is the UITableViewCell I have:

cell

The three UILabels have trailing, top, bottom and leading constraints.

name priority

location priority

type priority

On the viewDidLoad from my UITableViewController I'm doing something like this:

 self.tableView.estimatedRowHeight = 80
 self.tableView.rowHeight = UITableViewAutomaticDimension
 self.tableView.reloadData()

But when I run the app, the UITableViewCells don't self sizing:

result

What am I doing wrong or what do I need to do to make the UITableViewCells make them self sizing?

Edit

I set to 0 the numberOfLines for each UILabel, now the location UILabel doesn't appear

Upvotes: 0

Views: 340

Answers (4)

Carlo
Carlo

Reputation: 835

The label doesn't work because the storyboard is not very useful.

Set the var:

var imageView: UIImageView ={
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()

var textView: UITextView ={
let text = UITextView()
text.translatesAutoresizingMaskIntoConstraints = false
return text
}()

And you continue with other label.

You have to set constraints like this example:

self.addSubview(imageView)
self.addSubview(textView)

imageView.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor, constant: -10).active=true
imageView.leftAnchor.constraintEqualToAnchor(self.leftAnchor, constant:-10).active=true
imageView.widthAnchor.constraintEqualToConstant(70).active=true
imageView.heightAnchor.constraintEqualToConstant(70).active=true

label.topAnchor.constraintEqualToAnchor(self.topAnchor, constant: -5).active=true
label.leftAnchor.constraintEqualToAnchor(imageView.rightAnchor, constant: 3).active=true
label.widthAnchor.constraintEqualToAnchor(self.widthAnchor, constant -70).active=true
label.heightAnchor.constraintEqualToConstant(30).active=true

label2.topAnchor.constraintEqualToAnchor(label.bottomAnchor, constant: 5).active=true
label2.leftAnchor.constraintEqualToAnchor(imageView.rightAnchor, constant: 3).active=true
label2.widthAnchor.constraintEqualToAnchor(self.widthAnchor, constant -70).active=true
label2.heightAnchor.constraintEqualToConstant(30).active=true

And with this way also for the third label.

Upvotes: -1

DevKyle
DevKyle

Reputation: 1091

The question says you want to resize the label, but based on your code it seems like you want to do it by resizing the tableview rows. If that's the case, the correct place to set row height in a tableView is:

  override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    return 80

}

Where "return 80" means you want table rows to be of height 80. Please let me know if I didn't understand your question right.

Upvotes: 0

Pavankumar
Pavankumar

Reputation: 288

For ur case no need to vertical hugging priority,just give

label.numberOfLines = 0

that works fine.

Upvotes: 1

Zayne ZH
Zayne ZH

Reputation: 406

It is most likely that your Text turned out larger in runtime causing the total height in your cell View to turn out greater than the row height itself.

First equate all the hugging priority and then try changing the spacing distance between the Labels with the relation 'Greater than or Equal' and then set the constant to something small like zero. If you still get a constraint error, increase your tableView row height.

Alternative method: Add all 3 UILabels into another UIView with spacing zero between them, do not set a height constraint for this UIView and just set it in the centerY of the cellView and spaced from the ImageView

Tip: Only change constraint priorities if your UI is what you want even after the constraints are broken, unless of course you know what you're doing

Upvotes: 1

Related Questions