@@ -19,7 +19,7 @@ @interface FICDPhotosTableViewCell () <UIGestureRecognizerDelegate> {
19
19
NSArray *_photos;
20
20
NSString *_imageFormatName;
21
21
22
- NSMutableArray *_imageViews;
22
+ NSArray *_imageViews;
23
23
UITapGestureRecognizer *_tapGestureRecognizer;
24
24
}
25
25
@@ -39,42 +39,28 @@ @implementation FICDPhotosTableViewCell
39
39
- (void )setPhotos : (NSArray *)photos {
40
40
if (photos != _photos) {
41
41
_photos = [photos copy ];
42
-
43
- // Either create the image views for this cell or clear them out if they already exist
44
- if (_imageViews == nil ) {
45
- NSInteger photosPerRow = [[self class ] photosPerRow ];
46
- _imageViews = [[NSMutableArray alloc ] initWithCapacity: photosPerRow];
47
-
48
- for (NSInteger i = 0 ; i < photosPerRow; i++) {
49
- UIImageView *imageView = [[UIImageView alloc ] init ];
50
- [imageView setContentMode: UIViewContentModeScaleAspectFill];
51
- [_imageViews addObject: imageView];
52
- }
53
- } else {
54
- for (UIImageView *imageView in _imageViews) {
55
- [imageView setImage: nil ];
56
- [imageView removeFromSuperview ];
57
- }
58
- }
59
-
60
- NSInteger photosCount = [_photos count ];
61
- for (NSInteger i = 0 ; i < photosCount; i++) {
62
- FICDPhoto *photo = [_photos objectAtIndex: i];
42
+
43
+ for (NSInteger i = 0 ; i < [_imageViews count ]; i++) {
63
44
UIImageView *imageView = [_imageViews objectAtIndex: i];
64
-
65
- if (_usesImageTable) {
66
- [[FICImageCache sharedImageCache ] retrieveImageForEntity: photo withFormatName: _imageFormatName completionBlock: ^(id <FICEntity> entity, NSString *formatName, UIImage *image) {
67
- // This completion block may be called much later. We should check to make sure this cell hasn't been reused for different photos before displaying the image that has loaded.
68
- if (photos == [self photos ]) {
69
- [imageView setImage: image];
70
- }
71
- }];
45
+
46
+ if (i < [_photos count ]) {
47
+ FICDPhoto *photo = [_photos objectAtIndex: i];
48
+
49
+ if (_usesImageTable) {
50
+ [[FICImageCache sharedImageCache ] retrieveImageForEntity: photo withFormatName: _imageFormatName completionBlock: ^(id <FICEntity> entity, NSString *formatName, UIImage *image) {
51
+ // This completion block may be called much later. We should check to make sure this cell hasn't been reused for different photos before displaying the image that has loaded.
52
+ if (photos == [self photos ]) {
53
+ [imageView setImage: image];
54
+ }
55
+ }];
56
+ } else {
57
+ [imageView setImage: [photo thumbnailImage ]];
58
+ }
72
59
} else {
73
- [imageView setImage: [photo thumbnailImage ]];
60
+ // Last row might not be full
61
+ [imageView setImage: nil ];
74
62
}
75
63
}
76
-
77
- [self setNeedsLayout ];
78
64
}
79
65
}
80
66
@@ -117,6 +103,18 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
117
103
if (self != nil ) {
118
104
_tapGestureRecognizer = [[UITapGestureRecognizer alloc ] initWithTarget: self action: @selector (_tapGestureRecognizerStateDidChange )];
119
105
[self addGestureRecognizer: _tapGestureRecognizer];
106
+
107
+ NSInteger photosPerRow = [[self class ] photosPerRow ];
108
+ NSMutableArray *imageViews = [[NSMutableArray alloc ] initWithCapacity: photosPerRow];
109
+
110
+ for (NSInteger i = 0 ; i < photosPerRow; i++) {
111
+ UIImageView *imageView = [[UIImageView alloc ] init ];
112
+ [imageView setContentMode: UIViewContentModeScaleAspectFill];
113
+ [imageViews addObject: imageView];
114
+ [self .contentView addSubview: imageView];
115
+ }
116
+
117
+ _imageViews = [imageViews copy ];
120
118
}
121
119
122
120
return self;
@@ -139,14 +137,12 @@ - (void)layoutSubviews {
139
137
CGFloat outerPadding = [[self class ] outerPadding ];
140
138
141
139
CGRect imageViewFrame = CGRectMake (outerPadding, outerPadding, FICDPhotoSquareImageSize.width , FICDPhotoSquareImageSize.height );
142
-
143
- UIView *contentView = [self contentView ];
140
+
144
141
NSInteger count = [_photos count ];
145
142
146
143
for (NSInteger i = 0 ; i < count; i++) {
147
144
UIImageView *imageView = [_imageViews objectAtIndex: i];
148
145
[imageView setFrame: imageViewFrame];
149
- [contentView addSubview: imageView];
150
146
151
147
imageViewFrame.origin .x += imageViewFrame.size .width + innerPadding;
152
148
}
0 commit comments