@@ -132,14 +132,16 @@ - (id)fd_templateCellForReuseIdentifier:(NSString *)identifier
132
132
templateCell = [self dequeueReusableCellWithIdentifier: identifier];
133
133
NSAssert (templateCell != nil , @" Cell must be registered to table view for identifier - %@ " , identifier);
134
134
templateCell.fd_isTemplateLayoutCell = YES ;
135
+ templateCell.contentView .translatesAutoresizingMaskIntoConstraints = NO ;
135
136
templateCellsByIdentifiers[identifier] = templateCell;
136
137
[self fd_debugLog: [NSString stringWithFormat: @" layout cell created - %@ " , identifier]];
137
138
}
138
139
139
140
return templateCell;
140
141
}
141
142
142
- - (_FDTemplateLayoutCellHeightCache *)fd_cellHeightCache {
143
+ - (_FDTemplateLayoutCellHeightCache *)fd_cellHeightCache
144
+ {
143
145
_FDTemplateLayoutCellHeightCache *cache = objc_getAssociatedObject (self, _cmd);
144
146
if (!cache) {
145
147
cache = [_FDTemplateLayoutCellHeightCache new ];
@@ -230,15 +232,25 @@ - (void)fd_precacheIfNeeded
230
232
231
233
- (void )fd_precacheIndexPathIfNeeded : (NSIndexPath *)indexPath
232
234
{
233
- if (![self .fd_cellHeightCache hasCachedHeightAtIndexPath: indexPath]) {
234
- CGFloat height = [self .delegate tableView: self heightForRowAtIndexPath: indexPath];
235
- [self .fd_cellHeightCache cacheHeight: height byIndexPath: indexPath];
236
- [self fd_debugLog: [NSString stringWithFormat:
237
- @" precached - [%@ :%@ ] %@ " ,
238
- @(indexPath.section),
239
- @(indexPath.row),
240
- @(height)]];
235
+ // A cached indexPath
236
+ if ([self .fd_cellHeightCache hasCachedHeightAtIndexPath: indexPath]) {
237
+ return ;
238
+ }
239
+
240
+ // This RunLoop source may have been invalid at this point when data source
241
+ // changes during precache's dispatching.
242
+ if (indexPath.section >= [self numberOfSections ] ||
243
+ indexPath.row >= [self numberOfRowsInSection: indexPath.section]) {
244
+ return ;
241
245
}
246
+
247
+ CGFloat height = [self .delegate tableView: self heightForRowAtIndexPath: indexPath];
248
+ [self .fd_cellHeightCache cacheHeight: height byIndexPath: indexPath];
249
+ [self fd_debugLog: [NSString stringWithFormat:
250
+ @" precached - [%@ :%@ ] %@ " ,
251
+ @(indexPath.section),
252
+ @(indexPath.row),
253
+ @(height)]];
242
254
}
243
255
244
256
- (NSArray *)fd_allIndexPathsToBePrecached
@@ -342,6 +354,7 @@ - (void)fd_moveSection:(NSInteger)section toSection:(NSInteger)newSection
342
354
- (void )fd_insertRowsAtIndexPaths : (NSArray *)indexPaths withRowAnimation : (UITableViewRowAnimation)animation
343
355
{
344
356
if (self.fd_autoCacheInvalidationEnabled ) {
357
+ [self .fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded: indexPaths];
345
358
[indexPaths enumerateObjectsUsingBlock: ^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
346
359
NSMutableArray *rows = self.fd_cellHeightCache .sections [indexPath.section];
347
360
[rows insertObject: @(_FDTemplateLayoutCellHeightCacheAbsentValue) atIndex: indexPath.row];
@@ -364,6 +377,7 @@ - (void)fd_deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITabl
364
377
- (void )fd_reloadRowsAtIndexPaths : (NSArray *)indexPaths withRowAnimation : (UITableViewRowAnimation)animation
365
378
{
366
379
if (self.fd_autoCacheInvalidationEnabled ) {
380
+ [self .fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded: indexPaths];
367
381
[indexPaths enumerateObjectsUsingBlock: ^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
368
382
NSMutableArray *rows = self.fd_cellHeightCache .sections [indexPath.section];
369
383
rows[indexPath.row] = @(_FDTemplateLayoutCellHeightCacheAbsentValue);
@@ -376,6 +390,8 @@ - (void)fd_reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITabl
376
390
- (void )fd_moveRowAtIndexPath : (NSIndexPath *)sourceIndexPath toIndexPath : (NSIndexPath *)destinationIndexPath
377
391
{
378
392
if (self.fd_autoCacheInvalidationEnabled ) {
393
+ [self .fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded: @[sourceIndexPath, destinationIndexPath]];
394
+
379
395
NSMutableArray *sourceRows = self.fd_cellHeightCache .sections [sourceIndexPath.section];
380
396
NSMutableArray *destinationRows = self.fd_cellHeightCache .sections [destinationIndexPath.section];
381
397
0 commit comments