From 5ab40af1863057605396076a062583b6e851b77b Mon Sep 17 00:00:00 2001 From: changyiyao Date: Wed, 2 Dec 2015 12:19:29 +0800 Subject: [PATCH] Simplify auto layout for cell Remove unnessary preferredMaxLayoutWidth preferredMaxLayoutWidth. --- .../TableViewController/RJTableViewCell.m | 14 ---------- .../RJTableViewController.m | 26 +++++++++---------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/TableViewCellWithAutoLayout/TableViewController/RJTableViewCell.m b/TableViewCellWithAutoLayout/TableViewController/RJTableViewCell.m index 9e95db4..5ac2521 100644 --- a/TableViewCellWithAutoLayout/TableViewController/RJTableViewCell.m +++ b/TableViewCellWithAutoLayout/TableViewController/RJTableViewCell.m @@ -101,20 +101,6 @@ - (void)updateConstraints [super updateConstraints]; } -- (void)layoutSubviews -{ - [super layoutSubviews]; - - // Make sure the contentView does a layout pass here so that its subviews have their frames set, which we - // need to use to set the preferredMaxLayoutWidth below. - [self.contentView setNeedsLayout]; - [self.contentView layoutIfNeeded]; - - // Set the preferredMaxLayoutWidth of the mutli-line bodyLabel based on the evaluated width of the label's frame, - // as this will allow the text to wrap correctly, and as a result allow the label to take on the correct height. - self.bodyLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.bodyLabel.frame); -} - - (void)updateFonts { self.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; diff --git a/TableViewCellWithAutoLayout/TableViewController/RJTableViewController.m b/TableViewCellWithAutoLayout/TableViewController/RJTableViewController.m index 882497c..5fc3cdd 100644 --- a/TableViewCellWithAutoLayout/TableViewController/RJTableViewController.m +++ b/TableViewCellWithAutoLayout/TableViewController/RJTableViewController.m @@ -183,29 +183,29 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa [cell setNeedsUpdateConstraints]; [cell updateConstraintsIfNeeded]; - // The cell's width must be set to the same size it will end up at once it is in the table view. - // This is important so that we'll get the correct height for different table view widths, since our cell's - // height depends on its width due to the multi-line UILabel word wrapping. Don't need to do this above in - // -[tableView:cellForRowAtIndexPath:] because it happens automatically when the cell is used in the table view. - cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds)); + // NOTE: if you are displaying a section index (e.g. alphabet along the right side of the table view), or // if you are using a grouped table view style where cells have insets to the edges of the table view, // you'll need to adjust the cell.bounds.size.width to be smaller than the full width of the table view we just // set it to above. See http://stackoverflow.com/questions/3647242 for discussion on the section index width. - - // Do the layout pass on the cell, which will calculate the frames for all the views based on the constraints - // (Note that the preferredMaxLayoutWidth is set on multi-line UILabels inside the -[layoutSubviews] method - // in the UITableViewCell subclass - [cell setNeedsLayout]; - [cell layoutIfNeeded]; + CGFloat cellWidth = CGRectGetWidth(tableView.bounds); + + // Add a hard width constraint to make dynamic content views (like labels) expand vertically instead + // of growing horizontally, in a flow-layout manner. (Here you DON'T need setting preferredMaxLayoutWidth on multi-line UILabels inside the + // [layoutSubviews] method + NSLayoutConstraint *tempWidthConstraint = [NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:cellWidth]; + [cell.contentView addConstraint:tempWidthConstraint]; // Get the actual height required for the cell CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; + [cell.contentView removeConstraint:tempWidthConstraint]; // Add an extra point to the height to account for the cell separator, which is added between the bottom // of the cell's contentView and the bottom of the table view cell. - height += 1; - + if (self.tableView.separatorStyle != UITableViewCellSeparatorStyleNone) { + height += 1.f / [UIScreen mainScreen].scale; + } + return height; }