@@ -247,7 +247,7 @@ - (void)fd_precacheIndexPathIfNeeded:(NSIndexPath *)indexPath
247
247
CGFloat height = [self .delegate tableView: self heightForRowAtIndexPath: indexPath];
248
248
[self .fd_cellHeightCache cacheHeight: height byIndexPath: indexPath];
249
249
[self fd_debugLog: [NSString stringWithFormat:
250
- @" precached - [%@ :%@ ] %@ " ,
250
+ @" finished precache - [%@ :%@ ] %@ " ,
251
251
@(indexPath.section),
252
252
@(indexPath.row),
253
253
@(height)]];
@@ -427,28 +427,50 @@ - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(
427
427
configuration (cell);
428
428
}
429
429
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;
441
431
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 ;
444
434
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
+ }
446
462
447
463
// Add 1px extra space for separator line if needed, simulating default UITableViewCell.
448
464
if (self.separatorStyle != UITableViewCellSeparatorStyleNone) {
449
465
fittingSize.height += 1.0 / [UIScreen mainScreen ].scale ;
450
466
}
451
467
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
+
452
474
return fittingSize.height ;
453
475
}
454
476
@@ -479,10 +501,10 @@ - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier cacheByIndexPat
479
501
return [self .fd_cellHeightCache cachedHeightAtIndexPath: indexPath];
480
502
}
481
503
482
- // Do calculations
504
+ // Call basic height calculation method.
483
505
CGFloat height = [self fd_heightForCellWithIdentifier: identifier configuration: configuration];
484
506
[self fd_debugLog: [NSString stringWithFormat:
485
- @" calculate - [%@ :%@ ] %@ " ,
507
+ @" cached - [%@ :%@ ] %@ " ,
486
508
@(indexPath.section),
487
509
@(indexPath.row),
488
510
@(height)]];
0 commit comments