Skip to content

Commit 00d2350

Browse files
committed
feat(ios): improved cell size computation
BREAKING CHANGE: this needs testing to ensure it does not break anything for fixed size and dynamic size templates
1 parent 54d2eb9 commit 00d2350

File tree

9 files changed

+191
-36
lines changed

9 files changed

+191
-36
lines changed
228 Bytes
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#import <UIKit/UIKit.h>
2+
#import "UICollectionViewCacheDelegateFlowLayout.h"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@interface UICollectionViewCacheDelegateFlowLayout: NSObject <UICollectionViewDelegateFlowLayout>
2+
3+
@property (nonatomic, retain) NSMutableArray* cachedSizes;
4+
5+
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout computedSizeForItemAtIndexPath:(NSIndexPath *)indexPath;
6+
@end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#import "UICollectionViewCacheDelegateFlowLayout.h"
2+
3+
@implementation UICollectionViewCacheDelegateFlowLayout
4+
@synthesize cachedSizes;
5+
- (instancetype)init
6+
{
7+
self = [super init];
8+
if (self) {
9+
self.cachedSizes = [NSMutableArray new];
10+
}
11+
return self;
12+
}
13+
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout computedSizeForItemAtIndexPath:(NSIndexPath *)indexPath
14+
{
15+
return CGSizeZero;
16+
}
17+
18+
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
19+
{
20+
NSInteger index = indexPath.row;
21+
if (index < [self.cachedSizes count]) {
22+
NSValue* value = [self.cachedSizes objectAtIndex:indexPath.row];
23+
if (value != nil && !CGSizeEqualToSize([value CGSizeValue], CGSizeZero)) {
24+
return [value CGSizeValue];
25+
}
26+
CGSize size = [self collectionView:collectionView layout:collectionViewLayout computedSizeForItemAtIndexPath:indexPath];
27+
[self.cachedSizes replaceObjectAtIndex:indexPath.row withObject:[NSValue valueWithCGSize:size]];
28+
return size;
29+
} else {
30+
CGSize size = [self collectionView:collectionView layout:collectionViewLayout computedSizeForItemAtIndexPath:indexPath];
31+
[self.cachedSizes addObject:[NSValue valueWithCGSize:size]];
32+
return size;
33+
}
34+
}
35+
36+
@end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module NSCollectionView {
2+
umbrella header "NSCollectionView.h"
3+
export *
4+
module * { export * }
5+
}

src/collectionview/index.ios.ts

Lines changed: 137 additions & 35 deletions
Large diffs are not rendered by default.

src/collectionview/references.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/// <reference path="./typings/arv.d.ts" />
22
/// <reference path="./typings/android.d.ts" />
3+
/// <reference path="./typings/ios.d.ts" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare class UICollectionViewCacheDelegateFlowLayout extends NSObject implements UICollectionViewDelegateFlowLayout {
2+
cachedSizes: NSMutableArray;
3+
}

src/waterfall/index.ios.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default function install() {
5454
layout['minimumInteritemSpacing'] = 0;
5555
return layout;
5656
},
57-
createDelegate: () => UICollectionViewWaterfallDelegateImpl.new()
57+
createDelegate: (collectionview: CollectionView) => UICollectionViewWaterfallDelegateImpl.new()
5858
});
5959
CollectionView.registerPlugin('waterfall', {
6060
onLayout: (collectionview: CollectionView) => {

0 commit comments

Comments
 (0)