Skip to content

Commit 7dcd7be

Browse files
committed
Merge pull request forkingdog#18 from forkingdog/1.2-bugfix
Fix major bugs
2 parents e1a33db + 7a2a6e0 commit 7dcd7be

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

Classes/UITableView+FDTemplateLayoutCell.m

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,16 @@ - (id)fd_templateCellForReuseIdentifier:(NSString *)identifier
132132
templateCell = [self dequeueReusableCellWithIdentifier:identifier];
133133
NSAssert(templateCell != nil, @"Cell must be registered to table view for identifier - %@", identifier);
134134
templateCell.fd_isTemplateLayoutCell = YES;
135+
templateCell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
135136
templateCellsByIdentifiers[identifier] = templateCell;
136137
[self fd_debugLog:[NSString stringWithFormat:@"layout cell created - %@", identifier]];
137138
}
138139

139140
return templateCell;
140141
}
141142

142-
- (_FDTemplateLayoutCellHeightCache *)fd_cellHeightCache {
143+
- (_FDTemplateLayoutCellHeightCache *)fd_cellHeightCache
144+
{
143145
_FDTemplateLayoutCellHeightCache *cache = objc_getAssociatedObject(self, _cmd);
144146
if (!cache) {
145147
cache = [_FDTemplateLayoutCellHeightCache new];
@@ -230,15 +232,25 @@ - (void)fd_precacheIfNeeded
230232

231233
- (void)fd_precacheIndexPathIfNeeded:(NSIndexPath *)indexPath
232234
{
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;
241245
}
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)]];
242254
}
243255

244256
- (NSArray *)fd_allIndexPathsToBePrecached
@@ -342,6 +354,7 @@ - (void)fd_moveSection:(NSInteger)section toSection:(NSInteger)newSection
342354
- (void)fd_insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
343355
{
344356
if (self.fd_autoCacheInvalidationEnabled) {
357+
[self.fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded:indexPaths];
345358
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
346359
NSMutableArray *rows = self.fd_cellHeightCache.sections[indexPath.section];
347360
[rows insertObject:@(_FDTemplateLayoutCellHeightCacheAbsentValue) atIndex:indexPath.row];
@@ -364,6 +377,7 @@ - (void)fd_deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITabl
364377
- (void)fd_reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
365378
{
366379
if (self.fd_autoCacheInvalidationEnabled) {
380+
[self.fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded:indexPaths];
367381
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
368382
NSMutableArray *rows = self.fd_cellHeightCache.sections[indexPath.section];
369383
rows[indexPath.row] = @(_FDTemplateLayoutCellHeightCacheAbsentValue);
@@ -376,6 +390,8 @@ - (void)fd_reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITabl
376390
- (void)fd_moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
377391
{
378392
if (self.fd_autoCacheInvalidationEnabled) {
393+
[self.fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded:@[sourceIndexPath, destinationIndexPath]];
394+
379395
NSMutableArray *sourceRows = self.fd_cellHeightCache.sections[sourceIndexPath.section];
380396
NSMutableArray *destinationRows = self.fd_cellHeightCache.sections[destinationIndexPath.section];
381397

0 commit comments

Comments
 (0)