Autolayout and dynamic UILabel width inside Prototype Cell

I am trying to create a "3-column" UITableViewCell

where each column contains UILabel

. The two left labels are easy to set constraints, they are fixed width and their text will not exceed their IB width. Correct label, although it may have a long line in it, and I mean, "you can be as wide as you like, to the point where your width is within 8 pixels of the edge of the supervisor."

enter image description here

I came closer to the desired effect by adding

override func layoutSubviews() {
    ENTRY_LOG()

    log.debug("\(self.frame.width)")
    self.addConstraint(NSLayoutConstraint(item: self.destinationLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: self.frame.width - 151))
    self.setNeedsUpdateConstraints()

  }

      

in a custom class UITableViewCell

, but this now has the unwanted effect of breaking cell height and overwriting default rows between table rows.

+3


source to share


2 answers


Should it be problematic? A> = 8px trailing space for constiew constaint will do what you need.



Regarding your comment: "the second column is" squashed "because of its existence" - you should solve this problem by setting the "compression resistance priority" on the center label to a value higher than the rightmost label.

+1


source


If you do not want the height to be dictated by the inheritance height of the cell contents, you must specify it explicitly. The best way to do it is to set the row height directly in the table view:

tableView.rowHeight = 44

      



You can also specify it for specific cells using the tableView: heightForRowAtIndexPath table view delegate:

0


source







All Articles