Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify auto layout for cell #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions TableViewCellWithAutoLayout/TableViewController/RJTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down