Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for caching rotations #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions Classes/UITableView+FDTemplateLayoutCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ @interface _FDTemplateLayoutCellHeightCache : NSObject
// Tag a absent height cache value which will be set to a real value.
static CGFloat const _FDTemplateLayoutCellHeightCacheAbsentValue = -1;

#define IS_LANDSCAPE_ORIENTATION ((NSUInteger)UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))

@implementation _FDTemplateLayoutCellHeightCache

- (void)buildHeightCachesAtIndexPathsIfNeeded:(NSArray *)indexPaths
Expand All @@ -44,7 +46,7 @@ - (void)buildHeightCachesAtIndexPathsIfNeeded:(NSArray *)indexPaths
if (!self.sections) {
self.sections = @[].mutableCopy;
}

// Build every section array or row array which is smaller than given index path.
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
for (NSInteger section = 0; section <= indexPath.section; ++section) {
Expand All @@ -55,7 +57,7 @@ - (void)buildHeightCachesAtIndexPathsIfNeeded:(NSArray *)indexPaths
NSMutableArray *rows = self.sections[indexPath.section];
for (NSInteger row = 0; row <= indexPath.row; ++row) {
if (row >= rows.count) {
rows[row] = @(_FDTemplateLayoutCellHeightCacheAbsentValue);
rows[row] = @[@(_FDTemplateLayoutCellHeightCacheAbsentValue),@(_FDTemplateLayoutCellHeightCacheAbsentValue)].mutableCopy;
}
}
}];
Expand All @@ -64,23 +66,23 @@ - (void)buildHeightCachesAtIndexPathsIfNeeded:(NSArray *)indexPaths
- (BOOL)hasCachedHeightAtIndexPath:(NSIndexPath *)indexPath
{
[self buildHeightCachesAtIndexPathsIfNeeded:@[indexPath]];
NSNumber *cachedNumber = self.sections[indexPath.section][indexPath.row];
NSNumber *cachedNumber = self.sections[indexPath.section][indexPath.row][IS_LANDSCAPE_ORIENTATION];
return ![cachedNumber isEqualToNumber:@(_FDTemplateLayoutCellHeightCacheAbsentValue)];
}

- (void)cacheHeight:(CGFloat)height byIndexPath:(NSIndexPath *)indexPath
{
[self buildHeightCachesAtIndexPathsIfNeeded:@[indexPath]];
self.sections[indexPath.section][indexPath.row] = @(height);
self.sections[indexPath.section][indexPath.row][IS_LANDSCAPE_ORIENTATION] = @(height);
}

- (CGFloat)cachedHeightAtIndexPath:(NSIndexPath *)indexPath
{
[self buildHeightCachesAtIndexPathsIfNeeded:@[indexPath]];
#if CGFLOAT_IS_DOUBLE
return [self.sections[indexPath.section][indexPath.row] doubleValue];
return [self.sections[indexPath.section][indexPath.row][IS_LANDSCAPE_ORIENTATION] doubleValue];
#else
return [self.sections[indexPath.section][indexPath.row] floatValue];
return [self.sections[indexPath.section][indexPath.row][IS_LANDSCAPE_ORIENTATION] floatValue];
#endif
}

Expand Down Expand Up @@ -337,7 +339,7 @@ - (void)fd_reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRo
if (idx < self.fd_cellHeightCache.sections.count) {
NSMutableArray *rows = self.fd_cellHeightCache.sections[idx];
for (NSInteger row = 0; row < rows.count; ++row) {
rows[row] = @(_FDTemplateLayoutCellHeightCacheAbsentValue);
rows[row] = @[@(_FDTemplateLayoutCellHeightCacheAbsentValue),@(_FDTemplateLayoutCellHeightCacheAbsentValue)].mutableCopy;
}
}
}];
Expand All @@ -363,7 +365,7 @@ - (void)fd_insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITabl
[self.fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded:indexPaths];
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
NSMutableArray *rows = self.fd_cellHeightCache.sections[indexPath.section];
[rows insertObject:@(_FDTemplateLayoutCellHeightCacheAbsentValue) atIndex:indexPath.row];
[rows insertObject:@[@(_FDTemplateLayoutCellHeightCacheAbsentValue),@(_FDTemplateLayoutCellHeightCacheAbsentValue)].mutableCopy atIndex:indexPath.row];
}];
}
[self fd_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation]; // Primary call
Expand All @@ -388,7 +390,7 @@ - (void)fd_reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITabl
[self.fd_cellHeightCache buildHeightCachesAtIndexPathsIfNeeded:indexPaths];
[indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
NSMutableArray *rows = self.fd_cellHeightCache.sections[indexPath.section];
rows[indexPath.row] = @(_FDTemplateLayoutCellHeightCacheAbsentValue);
rows[indexPath.row] = @[@(_FDTemplateLayoutCellHeightCacheAbsentValue),@(_FDTemplateLayoutCellHeightCacheAbsentValue)].mutableCopy;
}];
}
[self fd_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation]; // Primary call
Expand All @@ -403,8 +405,8 @@ - (void)fd_moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSInde
NSMutableArray *sourceRows = self.fd_cellHeightCache.sections[sourceIndexPath.section];
NSMutableArray *destinationRows = self.fd_cellHeightCache.sections[destinationIndexPath.section];

NSNumber *sourceValue = sourceRows[sourceIndexPath.row];
NSNumber *destinationValue = destinationRows[destinationIndexPath.row];
id sourceValue = sourceRows[sourceIndexPath.row];
id destinationValue = destinationRows[destinationIndexPath.row];

sourceRows[sourceIndexPath.row] = destinationValue;
destinationRows[destinationIndexPath.row] = sourceValue;
Expand Down Expand Up @@ -536,7 +538,7 @@ - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier cacheByIndexPat
@(indexPath.section),
@(indexPath.row),
@(height)]];

// Cache it
[self.fd_cellHeightCache cacheHeight:height byIndexPath:indexPath];

Expand Down
5 changes: 3 additions & 2 deletions Demo/Demo/FDFeedViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ @implementation FDFeedViewController
- (void)viewDidLoad
{
[super viewDidLoad];

self.tableView.estimatedRowHeight = 200;

// Cells changing its size with tables with `estimatedRowHeight` doesn't work well with rotations.
// self.tableView.estimatedRowHeight = 200;
self.tableView.fd_debugLogEnabled = YES;

self.cellHeightCacheEnabled = YES;
Expand Down