Skip to content

Commit 957dcc0

Browse files
committed
Add frame layout support.
1 parent d1cd8f0 commit 957dcc0

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

Classes/UITableView+FDTemplateLayoutCell.m

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ - (void)fd_precacheIndexPathIfNeeded:(NSIndexPath *)indexPath
247247
CGFloat height = [self.delegate tableView:self heightForRowAtIndexPath:indexPath];
248248
[self.fd_cellHeightCache cacheHeight:height byIndexPath:indexPath];
249249
[self fd_debugLog:[NSString stringWithFormat:
250-
@"precached - [%@:%@] %@",
250+
@"finished precache - [%@:%@] %@",
251251
@(indexPath.section),
252252
@(indexPath.row),
253253
@(height)]];
@@ -427,28 +427,50 @@ - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(
427427
configuration(cell);
428428
}
429429

430-
// Add a hard width constraint to make dynamic content views (like labels) expand vertically instead
431-
// of growing horizontally, in a flow-layout manner.
432-
NSLayoutConstraint *tempWidthConstraint =
433-
[NSLayoutConstraint constraintWithItem:cell.contentView
434-
attribute:NSLayoutAttributeWidth
435-
relatedBy:NSLayoutRelationEqual
436-
toItem:nil
437-
attribute:NSLayoutAttributeNotAnAttribute
438-
multiplier:1.0
439-
constant:CGRectGetWidth(self.frame)];
440-
[cell.contentView addConstraint:tempWidthConstraint];
430+
CGSize fittingSize = CGSizeZero;
441431

442-
// Auto layout engine does its math
443-
CGSize fittingSize = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
432+
// If auto layout enabled, cell's contentView must have some constraints.
433+
BOOL autoLayoutEnabled = cell.contentView.constraints.count > 0 ? YES : NO;
444434

445-
[cell.contentView removeConstraint:tempWidthConstraint];
435+
if (autoLayoutEnabled) {
436+
437+
// Add a hard width constraint to make dynamic content views (like labels) expand vertically instead
438+
// of growing horizontally, in a flow-layout manner.
439+
NSLayoutConstraint *tempWidthConstraint =
440+
[NSLayoutConstraint constraintWithItem:cell.contentView
441+
attribute:NSLayoutAttributeWidth
442+
relatedBy:NSLayoutRelationEqual
443+
toItem:nil
444+
attribute:NSLayoutAttributeNotAnAttribute
445+
multiplier:1.0
446+
constant:CGRectGetWidth(self.frame)];
447+
[cell.contentView addConstraint:tempWidthConstraint];
448+
// Auto layout engine does its math
449+
fittingSize = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
450+
[cell.contentView removeConstraint:tempWidthConstraint];
451+
452+
} else {
453+
454+
// If not using auto layout, you have to override "-sizeThatFits:" to provide a fitting size by yourself.
455+
// This is the same method used in iOS8 self-sizing cell's implementation.
456+
// Note: fitting height should not include separator view.
457+
SEL selector = @selector(sizeThatFits:);
458+
BOOL overrided = [cell.class instanceMethodForSelector:selector] != [UITableViewCell instanceMethodForSelector:selector];
459+
NSAssert(overrided, @"Cell must override '-sizeThatFits:' method if not using auto layout.");
460+
fittingSize = [cell sizeThatFits:self.frame.size];
461+
}
446462

447463
// Add 1px extra space for separator line if needed, simulating default UITableViewCell.
448464
if (self.separatorStyle != UITableViewCellSeparatorStyleNone) {
449465
fittingSize.height += 1.0 / [UIScreen mainScreen].scale;
450466
}
451467

468+
if (autoLayoutEnabled) {
469+
[self fd_debugLog:[NSString stringWithFormat:@"calculate using auto layout - %@", @(fittingSize.height)]];
470+
} else {
471+
[self fd_debugLog:[NSString stringWithFormat:@"calculate using frame layout - %@", @(fittingSize.height)]];
472+
}
473+
452474
return fittingSize.height;
453475
}
454476

@@ -479,10 +501,10 @@ - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier cacheByIndexPat
479501
return [self.fd_cellHeightCache cachedHeightAtIndexPath:indexPath];
480502
}
481503

482-
// Do calculations
504+
// Call basic height calculation method.
483505
CGFloat height = [self fd_heightForCellWithIdentifier:identifier configuration:configuration];
484506
[self fd_debugLog:[NSString stringWithFormat:
485-
@"calculate - [%@:%@] %@",
507+
@"cached - [%@:%@] %@",
486508
@(indexPath.section),
487509
@(indexPath.row),
488510
@(height)]];

Demo/Demo/FDFeedCell.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ - (void)setEntity:(FDFeedEntity *)entity
3939
self.timeLabel.text = entity.time;
4040
}
4141

42+
// If you are not using auto layout, override this method
43+
//- (CGSize)sizeThatFits:(CGSize)size
44+
//{
45+
// return CGSizeMake(size.width, 150);
46+
//}
47+
4248
@end

0 commit comments

Comments
 (0)