Skip to content

Commit c03d877

Browse files
committed
Cleaned up comments, reorganized code slightly.
1 parent 8142e56 commit c03d877

2 files changed

Lines changed: 7 additions & 65 deletions

File tree

TableViewCellWithAutoLayout/TableViewController/RJTableViewCell.m

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
@interface RJTableViewCell ()
1212

13-
// We'll set this to YES once this instance of the cell has setup its constraints, so that any additional calls to updateConstraints are no-ops
1413
@property (nonatomic, assign) BOOL hasSetupConstraints;
1514

1615
@end
@@ -31,33 +30,17 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
3130
[self.titleLabel setFont: [UIFont fontWithName:@"Helvetica-Bold" size:21]];
3231
[self.titleLabel setTextColor:[UIColor blackColor]];
3332
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
34-
// [self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
35-
// [self.titleLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
36-
33+
3734
self.bodyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
3835
[self.bodyLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
36+
[self.bodyLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
3937
[self.bodyLabel setLineBreakMode:NSLineBreakByTruncatingTail];
40-
41-
// You don't want the below line; let the font size for the multi-line body label remain fixed.
42-
// [self.titleLabel setAdjustsFontSizeToFitWidth:YES];
43-
4438
[self.bodyLabel setNumberOfLines:0];
4539
[self.bodyLabel setTextAlignment:NSTextAlignmentLeft];
4640
[self.bodyLabel setFont: [UIFont fontWithName:@"Helvetica" size:14]];
4741
[self.bodyLabel setTextColor:[UIColor darkGrayColor]];
4842
[self.bodyLabel setBackgroundColor:[UIColor clearColor]];
49-
50-
// If you don't do this, the auto layout solver engine may calculate a ever-so-slightly smaller height than actually required
51-
// (probably due to rounding errors internally) which will cause the bodyLabel to truncate the last line. This will force
52-
// the bodyLabel to always get the size it needs for all the lines. (The titleLabel height will be reduced ever so slightly
53-
// in this case if needed to satisfy this constraint, but in this case it shouldn't be noticeable.)
54-
// Another way to solve this is to return a slightly larger cell height (add 1 point) than the auto layout calculations return.
55-
// Or a third way is to build in a few points of padding (perhaps between the labels, or above or below them) which has a priority of
56-
// UILayoutPriorityDefaultHigh - 1, so that it will be the first thing to be reduced if the cell height is a tiny bit too small -
57-
// it will act as a buffer to prevent the labels from getting smaller than the size they really want.
58-
[self.bodyLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
5943

60-
// Add your subviews right away during init - no reason to wait until later
6144
[self.contentView addSubview:self.titleLabel];
6245
[self.contentView addSubview:self.bodyLabel];
6346
}
@@ -68,8 +51,6 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
6851
- (void)updateConstraints {
6952
[super updateConstraints];
7053

71-
// Modified to a single-line return. No biggie.
72-
//
7354
if (self.hasSetupConstraints) return;
7455

7556
[self.contentView addConstraint:[NSLayoutConstraint
@@ -99,31 +80,7 @@ - (void)updateConstraints {
9980
multiplier:1.0f
10081
constant:-kLabelHorizontalInsets]];
10182

102-
// You don't need the below constraint - the auto-generated content compression resistance constraints on UILabel are sufficient!
103-
// [self.contentView addConstraint:[NSLayoutConstraint
104-
// constraintWithItem:self.titleLabel
105-
// attribute:NSLayoutAttributeHeight
106-
// relatedBy:NSLayoutRelationGreaterThanOrEqual
107-
// toItem:nil
108-
// attribute:NSLayoutAttributeNotAnAttribute
109-
// multiplier:1.0f
110-
// constant:22.0f]];
111-
112-
// "You don't need the below constraint - the auto-generated content compression resistance constraints on UILabel are sufficient!"
113-
//
114-
// OK, this is weird. Without NSLayoutAttributeHeight-NSLayoutRelationGreaterThanOrEqual self.titleLabel collapses?
115-
// Adding the NSLayoutAttributeHeight-NSLayoutRelationGreaterThanOrEqual resolves issue. Perhaps there is another way...
116-
// ...
117-
[self.contentView addConstraint:[NSLayoutConstraint
118-
constraintWithItem:self.titleLabel
119-
attribute:NSLayoutAttributeHeight
120-
relatedBy:NSLayoutRelationGreaterThanOrEqual
121-
toItem:nil
122-
attribute:NSLayoutAttributeNotAnAttribute
123-
multiplier:1.0f
124-
constant:22.0f]];
125-
126-
///////////////////////////////////////////////////////////////////////////////////////////
83+
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
12784

12885
[self.contentView addConstraint:[NSLayoutConstraint
12986
constraintWithItem:self.bodyLabel
@@ -161,16 +118,6 @@ - (void)updateConstraints {
161118
multiplier:1.0f
162119
constant:-(kLabelHorizontalInsets / 2)]];
163120

164-
// You don't need the below constraint - the auto-generated content compression resistance constraints on UILabel are sufficient!
165-
// [self.contentView addConstraint:[NSLayoutConstraint
166-
// constraintWithItem:self.bodyLabel
167-
// attribute:NSLayoutAttributeHeight
168-
// relatedBy:NSLayoutRelationGreaterThanOrEqual
169-
// toItem:nil
170-
// attribute:NSLayoutAttributeNotAnAttribute
171-
// multiplier:1.0f
172-
// constant:22.0f]];
173-
174121
self.hasSetupConstraints = YES;
175122

176123
}

TableViewCellWithAutoLayout/TableViewController/RJTableViewController.m

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,14 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
6767
{
6868

6969
RJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
70-
71-
NSDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:indexPath.row];
7270

7371
// Configure the cell...
72+
73+
NSDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:indexPath.row];
74+
7475
cell.titleLabel.text = [dataSourceItem valueForKey:@"title"];
7576
cell.bodyLabel.text = [dataSourceItem valueForKey:@"body"];
7677

77-
// Don't need to set the preferredMaxLayoutWidth here because the cell will now have a fixed width, which will cause
78-
// the multi-line label to wrap correctly since it is pinned to the left & right edges of the cell's contentView.
79-
// cell.bodyLabel.preferredMaxLayoutWidth = tableView.bounds.size.width - kLabelHorizontalInsets * 2.0f;
80-
8178
// Make sure the constraints have been added to this cell, since it may have just been created from scratch
8279
[cell setNeedsUpdateConstraints];
8380

@@ -100,6 +97,7 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
10097
// Configure the cell with content for the given indexPath, for example:
10198
// cell.textLabel.text = someTextForThisCell;
10299
// ...
100+
103101
NSDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:indexPath.row];
104102
cell.titleLabel.text = [dataSourceItem valueForKey:@"title"];
105103
cell.bodyLabel.text = [dataSourceItem valueForKey:@"body"];
@@ -116,17 +114,14 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
116114

117115
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
118116

119-
NSLog(@"My returned height = %f", height);
120117
return height;
121118
}
122119

123120
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
124121
{
125122
// Return a fixed constant if possible, or do some minimal calculations if needed to be able to return an
126123
// estimated row height that's at least within an order of magnitude of the actual height.
127-
// For example:
128124

129-
// NSLog(@"%@ %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
130125
return 500.0f;
131126

132127
}

0 commit comments

Comments
 (0)