Skip to content

Commit 3706c93

Browse files
committed
Allows customizing number of thumbnails in a row.
1 parent 3147e34 commit 3706c93

File tree

7 files changed

+82
-78
lines changed

7 files changed

+82
-78
lines changed

Demo/Demo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,15 @@
345345
isa = XCBuildConfiguration;
346346
buildSettings = {
347347
ALWAYS_SEARCH_USER_PATHS = NO;
348+
CODE_SIGN_IDENTITY = "iPhone Distribution: Sogeti Nederland B.V.";
349+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution: Sogeti Nederland B.V.";
348350
COPY_PHASE_STRIP = YES;
349351
GCC_PRECOMPILE_PREFIX_HEADER = YES;
350352
GCC_PREFIX_HEADER = "Demo/Demo-Prefix.pch";
351353
INFOPLIST_FILE = "Demo/Demo-Info.plist";
352354
PRODUCT_NAME = "$(TARGET_NAME)";
355+
PROVISIONING_PROFILE = "B829716D-BAB9-482A-8361-DA28B2CB01BE";
356+
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "B829716D-BAB9-482A-8361-DA28B2CB01BE";
353357
VALIDATE_PRODUCT = YES;
354358
WRAPPER_EXTENSION = app;
355359
};

Demo/Demo/Demo-Info.plist

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
<key>CFBundleDevelopmentRegion</key>
66
<string>en</string>
77
<key>CFBundleDisplayName</key>
8-
<string>${PRODUCT_NAME}</string>
8+
<string>Grid</string>
9+
<key>CFBundleDocumentTypes</key>
10+
<array/>
911
<key>CFBundleExecutable</key>
1012
<string>${EXECUTABLE_NAME}</string>
1113
<key>CFBundleIconFile</key>
@@ -22,6 +24,8 @@
2224
<string>1.0</string>
2325
<key>CFBundleSignature</key>
2426
<string>????</string>
27+
<key>CFBundleURLTypes</key>
28+
<array/>
2529
<key>CFBundleVersion</key>
2630
<string>1.0</string>
2731
<key>LSRequiresIPhoneOS</key>
@@ -34,5 +38,9 @@
3438
<string>UIInterfaceOrientationLandscapeLeft</string>
3539
<string>UIInterfaceOrientationLandscapeRight</string>
3640
</array>
41+
<key>UTExportedTypeDeclarations</key>
42+
<array/>
43+
<key>UTImportedTypeDeclarations</key>
44+
<array/>
3745
</dict>
3846
</plist>

Demo/Demo/RootViewController.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ - (NSInteger)numberOfThumbnailsInThumbnailGridView:(VCThumbnailGridView*)thumbna
144144
return 22;
145145
}
146146

147+
- (NSInteger)numberOfThumbnailsInRowForThumbnailGridView:(VCThumbnailGridView *)thumbnailGridView
148+
{
149+
return 2;
150+
}
151+
147152
- (UIImage*)thumbnailGridView:(VCThumbnailGridView*)thumbnailGridView imageAtIndex:(NSInteger)index
148153
{
149154
return [UIImage imageNamed:@"Icon.png"];

VCThumbnailGridView/VCThumbnailGridView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
UITableView *_tableView;
4848
NSInteger _numberOfThumbnails;
49+
NSInteger _numberOfThumbnailsInRow;
4950
BOOL _isEditing;
5051
}
5152

@@ -65,6 +66,7 @@
6566
- (NSInteger)numberOfThumbnailsInThumbnailGridView:(VCThumbnailGridView*)thumbnailGridView;
6667

6768
@optional
69+
- (NSInteger)numberOfThumbnailsInRowForThumbnailGridView:(VCThumbnailGridView *)thumbnailGridView;
6870
- (UIImage*)thumbnailGridView:(VCThumbnailGridView*)thumbnailGridView imageAtIndex:(NSInteger)index;
6971
- (NSString*)thumbnailGridView:(VCThumbnailGridView*)thumbnailGridView imageUrlAtIndex:(NSInteger)index;
7072

VCThumbnailGridView/VCThumbnailGridView.m

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ - (id)initWithFrame:(CGRect)frame {
4949
_tableView.clipsToBounds = NO;
5050
_tableView.dataSource = self;
5151
_tableView.delegate = self;
52-
_tableView.rowHeight = 79; // 75 + 2 + 2
52+
_tableView.rowHeight = 0;
5353
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
5454
[self addSubview:_tableView];
5555
}
@@ -83,6 +83,14 @@ - (void)reloadData {
8383
}
8484
_numberOfThumbnails = MAX(_numberOfThumbnails, 0);
8585

86+
if ([self.dataSource respondsToSelector:@selector(numberOfThumbnailsInRowForThumbnailGridView:)]) {
87+
_numberOfThumbnailsInRow = [self.dataSource numberOfThumbnailsInRowForThumbnailGridView:self];
88+
}
89+
_numberOfThumbnailsInRow = MAX(_numberOfThumbnailsInRow, 0);
90+
91+
CGFloat width = (320 - (4 * (_numberOfThumbnailsInRow+1))) / _numberOfThumbnailsInRow;
92+
_tableView.rowHeight = width + 4;
93+
8694
[_tableView reloadData];
8795
}
8896

@@ -103,8 +111,8 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
103111
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
104112
{
105113
// Return the number of rows in the section.
106-
NSInteger numberOfCells = _numberOfThumbnails / 4;
107-
if (_numberOfThumbnails % 4 != 0) {
114+
NSInteger numberOfCells = _numberOfThumbnails / _numberOfThumbnailsInRow;
115+
if (_numberOfThumbnails % _numberOfThumbnailsInRow != 0) {
108116
numberOfCells += 1;
109117
}
110118

@@ -117,52 +125,43 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
117125

118126
VCThumbnailViewCell *cell = (VCThumbnailViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
119127
if (cell == nil) {
120-
cell = [[[VCThumbnailViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
128+
cell = [[[VCThumbnailViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier thumbnailCount:_numberOfThumbnailsInRow] autorelease];
121129
cell.accessoryType = UITableViewCellAccessoryNone;
122130
cell.selectionStyle = UITableViewCellSelectionStyleNone;
123131
}
124132

125133
// Configure the cell...
126-
int indexOfImage = indexPath.row * 4;
127-
if ([self.dataSource respondsToSelector:@selector(thumbnailGridView:imageAtIndex:)]) {
128-
if (indexOfImage < _numberOfThumbnails) {
129-
[cell.imageView1 setImage:[self.dataSource thumbnailGridView:self imageAtIndex:indexOfImage]];
130-
cell.imageView1.tag = indexOfImage++;
131-
[cell.imageView1 addTarget:self withSelector:@selector(didTapImageThumbnail:)];
132-
}
133-
if (indexOfImage < _numberOfThumbnails) {
134-
[cell.imageView2 setImage:[self.dataSource thumbnailGridView:self imageAtIndex:indexOfImage]];
135-
cell.imageView2.tag = indexOfImage++;
136-
[cell.imageView2 addTarget:self withSelector:@selector(didTapImageThumbnail:)];
137-
}
138-
if (indexOfImage < _numberOfThumbnails) {
139-
[cell.imageView3 setImage:[self.dataSource thumbnailGridView:self imageAtIndex:indexOfImage]];
140-
cell.imageView3.tag = indexOfImage++;
141-
[cell.imageView3 addTarget:self withSelector:@selector(didTapImageThumbnail:)];
142-
}
143-
if (indexOfImage < _numberOfThumbnails) {
144-
[cell.imageView4 setImage:[self.dataSource thumbnailGridView:self imageAtIndex:indexOfImage]];
145-
cell.imageView4.tag = indexOfImage++;
146-
[cell.imageView4 addTarget:self withSelector:@selector(didTapImageThumbnail:)];
147-
}
148-
}
134+
int indexOfImage = indexPath.row * _numberOfThumbnailsInRow;
149135

150-
indexOfImage = indexPath.row * 4;
151-
if ([self.dataSource respondsToSelector:@selector(thumbnailGridView:imageUrlAtIndex:)]) {
152-
if (indexOfImage < _numberOfThumbnails) {
153-
[cell.imageView1 setImageUrl:[self.dataSource thumbnailGridView:self imageUrlAtIndex:indexOfImage++]];
154-
}
155-
if (indexOfImage < _numberOfThumbnails) {
156-
[cell.imageView2 setImageUrl:[self.dataSource thumbnailGridView:self imageUrlAtIndex:indexOfImage++]];
157-
}
158-
if (indexOfImage < _numberOfThumbnails) {
159-
[cell.imageView3 setImageUrl:[self.dataSource thumbnailGridView:self imageUrlAtIndex:indexOfImage++]];
160-
}
161-
if (indexOfImage < _numberOfThumbnails) {
162-
[cell.imageView4 setImageUrl:[self.dataSource thumbnailGridView:self imageUrlAtIndex:indexOfImage++]];
136+
VCThumbnailButton *thumbnail = nil;
137+
for (int i = 0; i < _numberOfThumbnailsInRow; i++) {
138+
thumbnail = [cell.thumbnails objectAtIndex:i];
139+
140+
if ([self.dataSource respondsToSelector:@selector(thumbnailGridView:imageAtIndex:)]) {
141+
if (indexOfImage < _numberOfThumbnails) {
142+
[thumbnail setImage:[self.dataSource thumbnailGridView:self imageAtIndex:indexOfImage]];
143+
thumbnail.tag = indexOfImage++;
144+
[thumbnail addTarget:self withSelector:@selector(didTapImageThumbnail:)];
145+
}
163146
}
164147
}
165148

149+
//
150+
// if ([self.dataSource respondsToSelector:@selector(thumbnailGridView:imageUrlAtIndex:)]) {
151+
// if (indexOfImage < _numberOfThumbnails) {
152+
// [cell.imageView1 setImageUrl:[self.dataSource thumbnailGridView:self imageUrlAtIndex:indexOfImage++]];
153+
// }
154+
// if (indexOfImage < _numberOfThumbnails) {
155+
// [cell.imageView2 setImageUrl:[self.dataSource thumbnailGridView:self imageUrlAtIndex:indexOfImage++]];
156+
// }
157+
// if (indexOfImage < _numberOfThumbnails) {
158+
// [cell.imageView3 setImageUrl:[self.dataSource thumbnailGridView:self imageUrlAtIndex:indexOfImage++]];
159+
// }
160+
// if (indexOfImage < _numberOfThumbnails) {
161+
// [cell.imageView4 setImageUrl:[self.dataSource thumbnailGridView:self imageUrlAtIndex:indexOfImage++]];
162+
// }
163+
// }
164+
166165
return cell;
167166
}
168167

VCThumbnailGridView/VCThumbnailViewCell.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@
2929
#import "VCThumbnailButton.h"
3030

3131
@interface VCThumbnailViewCell : UITableViewCell {
32-
VCThumbnailButton *imageView1;
33-
VCThumbnailButton *imageView2;
34-
VCThumbnailButton *imageView3;
35-
VCThumbnailButton *imageView4;
32+
NSMutableArray *thumbnails;
3633
}
3734

38-
@property (nonatomic, retain) VCThumbnailButton *imageView1;
39-
@property (nonatomic, retain) VCThumbnailButton *imageView2;
40-
@property (nonatomic, retain) VCThumbnailButton *imageView3;
41-
@property (nonatomic, retain) VCThumbnailButton *imageView4;
35+
@property (nonatomic, readonly) NSArray *thumbnails;
36+
37+
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier thumbnailCount:(NSInteger)count;
4238

4339
@end

VCThumbnailGridView/VCThumbnailViewCell.m

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,26 @@
2828

2929
@implementation VCThumbnailViewCell
3030

31-
@synthesize imageView1, imageView2, imageView3, imageView4;
31+
@synthesize thumbnails;
3232

33-
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
33+
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier thumbnailCount:(NSInteger)count
3434
{
3535
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
3636
if (self) {
37-
// Initialization code
38-
imageView1 = [[VCThumbnailButton alloc] initWithFrame:CGRectMake(4, 2, 75, 75)];
39-
imageView1.backgroundColor = [UIColor whiteColor];
40-
imageView1.shouldShowActivityIndicator = YES;
41-
[self addSubview:imageView1];
37+
CGFloat currentX = 0.0f;
38+
CGFloat width = (320 - (4 * (count+1))) / count;
4239

43-
imageView2 = [[VCThumbnailButton alloc] initWithFrame:CGRectMake(83, 2, 75, 75)];
44-
imageView2.backgroundColor = [UIColor whiteColor];
45-
imageView2.shouldShowActivityIndicator = YES;
46-
[self addSubview:imageView2];
47-
48-
imageView3 = [[VCThumbnailButton alloc] initWithFrame:CGRectMake(162, 2, 75, 75)];
49-
imageView3.backgroundColor = [UIColor whiteColor];
50-
imageView3.shouldShowActivityIndicator = YES;
51-
[self addSubview:imageView3];
52-
53-
imageView4 = [[VCThumbnailButton alloc] initWithFrame:CGRectMake(241, 2, 75, 75)];
54-
imageView4.backgroundColor = [UIColor whiteColor];
55-
imageView4.shouldShowActivityIndicator = YES;
56-
[self addSubview:imageView4];
40+
thumbnails = [[NSMutableArray alloc] initWithCapacity:count];
5741

42+
VCThumbnailButton *thumbnailButton = nil;
43+
for (int counter = 0; counter < count; counter++) {
44+
thumbnailButton = [[VCThumbnailButton alloc] initWithFrame:CGRectMake(4 + (counter * (width+4)), 2, width, width)];
45+
thumbnailButton.backgroundColor = [UIColor whiteColor];
46+
thumbnailButton.shouldShowActivityIndicator = YES;
47+
[thumbnails addObject:thumbnailButton];
48+
[self addSubview:thumbnailButton];
49+
[thumbnailButton release], thumbnailButton = nil;
50+
}
5851
}
5952
return self;
6053
}
@@ -64,18 +57,15 @@ - (void)setEditing:(BOOL)editing animated:(BOOL)animated
6457
[super setEditing:editing animated:animated];
6558

6659
// Configure the view for the selected state
67-
[imageView1 setEditing:editing animated:animated];
68-
[imageView2 setEditing:editing animated:animated];
69-
[imageView3 setEditing:editing animated:animated];
70-
[imageView4 setEditing:editing animated:animated];
60+
for (VCThumbnailButton *thumbnail in thumbnails) {
61+
[thumbnail setEditing:editing animated:animated];
62+
}
7163
}
7264

7365
- (void)dealloc
7466
{
75-
[imageView1 release];
76-
[imageView2 release];
77-
[imageView3 release];
78-
[imageView4 release];
67+
[thumbnails removeAllObjects];
68+
[thumbnails release], thumbnails = nil;
7969
[super dealloc];
8070
}
8171

0 commit comments

Comments
 (0)