Skip to content

Commit 285d266

Browse files
committed
Force frame layout support
1 parent abd39c2 commit 285d266

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

Classes/UITableView+FDTemplateLayoutCell.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,13 @@
7575
///
7676
@property (nonatomic, assign) BOOL fd_isTemplateLayoutCell;
7777

78+
/// Enable to enforce this template layout cell to use "frame layout" rather than "auto layout",
79+
/// and will ask cell's height by calling "-sizeThatFits:", so you must override this method.
80+
/// Note:
81+
/// If no layout constraints have been added to cell's content view, it will automatically
82+
/// switch to "frame layout" mode. Use this property only when you want to manually control
83+
/// this template layout cell's height calculation mode. Default to NO.
84+
///
85+
@property (nonatomic, assign) BOOL fd_enforceFrameLayout;
86+
7887
@end

Classes/UITableView+FDTemplateLayoutCell.m

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,7 @@ - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(
437437
CGSize fittingSize = CGSizeZero;
438438

439439
// If auto layout enabled, cell's contentView must have some constraints.
440-
BOOL autoLayoutEnabled = cell.contentView.constraints.count > 0 ? YES : NO;
441-
440+
BOOL autoLayoutEnabled = cell.contentView.constraints.count > 0 && !cell.fd_enforceFrameLayout;
442441
if (autoLayoutEnabled) {
443442

444443
// Add a hard width constraint to make dynamic content views (like labels) expand vertically instead
@@ -462,8 +461,11 @@ - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(
462461
// This is the same method used in iOS8 self-sizing cell's implementation.
463462
// Note: fitting height should not include separator view.
464463
SEL selector = @selector(sizeThatFits:);
464+
BOOL inherited = ![cell isMemberOfClass:UITableViewCell.class];
465465
BOOL overrided = [cell.class instanceMethodForSelector:selector] != [UITableViewCell instanceMethodForSelector:selector];
466-
NSAssert(overrided, @"Cell must override '-sizeThatFits:' method if not using auto layout.");
466+
if (inherited && !overrided) {
467+
NSAssert(NO, @"Customized cell must override '-sizeThatFits:' method if not using auto layout.");
468+
}
467469
fittingSize = [cell sizeThatFits:self.frame.size];
468470
}
469471

@@ -548,4 +550,14 @@ - (void)setFd_isTemplateLayoutCell:(BOOL)isTemplateLayoutCell
548550
objc_setAssociatedObject(self, @selector(fd_isTemplateLayoutCell), @(isTemplateLayoutCell), OBJC_ASSOCIATION_RETAIN);
549551
}
550552

553+
- (BOOL)fd_enforceFrameLayout
554+
{
555+
return [objc_getAssociatedObject(self, _cmd) boolValue];
556+
}
557+
558+
- (void)setFd_enforceFrameLayout:(BOOL)enforceFrameLayout
559+
{
560+
objc_setAssociatedObject(self, @selector(fd_enforceFrameLayout), @(enforceFrameLayout), OBJC_ASSOCIATION_RETAIN);
561+
}
562+
551563
@end

Demo/Demo/FDFeedCell.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ - (void)setEntity:(FDFeedEntity *)entity
4040
}
4141

4242
// If you are not using auto layout, override this method
43-
//- (CGSize)sizeThatFits:(CGSize)size
44-
//{
45-
// return CGSizeMake(size.width, 150);
46-
//}
43+
- (CGSize)sizeThatFits:(CGSize)size
44+
{
45+
CGFloat totalHeight = 0;
46+
totalHeight += [self.titleLabel sizeThatFits:size].height;
47+
totalHeight += [self.contentLabel sizeThatFits:size].height;
48+
totalHeight += [self.contentImageView sizeThatFits:size].height;
49+
totalHeight += [self.usernameLabel sizeThatFits:size].height;
50+
totalHeight += 40; // margins
51+
return CGSizeMake(size.width, totalHeight);
52+
}
4753

4854
@end

Demo/Demo/FDFeedViewController.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
8181

8282
- (void)configureCell:(FDFeedCell *)cell atIndexPath:(NSIndexPath *)indexPath
8383
{
84+
cell.fd_enforceFrameLayout = NO; // Enable to use "-sizeThatFits:"
8485
cell.entity = self.feedEntitySections[indexPath.section][indexPath.row];
8586
}
8687

@@ -162,6 +163,9 @@ - (FDFeedEntity *)randomEntity
162163

163164
- (void)insertRow
164165
{
166+
if (self.feedEntitySections.count == 0) {
167+
self.feedEntitySections[0] = @[].mutableCopy;
168+
}
165169
[self.feedEntitySections[0] insertObject:self.randomEntity atIndex:0];
166170
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
167171
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

0 commit comments

Comments
 (0)