Skip to content
Newer
Older
100644 480 lines (366 sloc) 13.6 KB
498b075 @camh first commit
authored Feb 22, 2010
1 //
2 // CHGridView.m
3 //
8052aca @camh Changed code header information (added a line about license) and README
authored Feb 22, 2010
4 // RELEASED UNDER THE MIT LICENSE
5 //
498b075 @camh first commit
authored Feb 22, 2010
6 // Created by Cameron Kenly Hunt on 2/18/10.
7 // Copyright 2010 Cameron Kenley Hunt All rights reserved.
8 // http://cameron.io/project/chgridview
9 //
10
11 #import "CHGridView.h"
db6118e @camh Performance enhancements.
authored Feb 25, 2010
12 #import "CHGridLayoutTile.h"
498b075 @camh first commit
authored Feb 22, 2010
13
14 @interface CHGridView()
15 - (void)loadVisibleSectionTitlesForSectionRange:(CHSectionRange)range;
db6118e @camh Performance enhancements.
authored Feb 25, 2010
16 - (void)loadVisibleTileForIndexPath:(CHGridIndexPath)indexPath withRect:(CGRect)r;
61e3b0e @camh Improved reusable cells converter and changed README
authored Feb 22, 2010
17 - (void)reuseHiddenTiles;
498b075 @camh first commit
authored Feb 22, 2010
18 - (void)removeSectionTitleNotInRange:(CHSectionRange)range;
19 - (void)removeAllSubviews;
20 - (NSMutableArray *)tilesForSection:(int)section;
21 - (NSMutableArray *)tilesFromIndex:(int)startIndex toIndex:(int)endIndex inSection:(int)section;
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 1, 2010
22 - (CHSectionHeaderView *)sectionHeaderViewForSection:(int)section;
498b075 @camh first commit
authored Feb 22, 2010
23 - (void)calculateSectionTitleOffset;
24 @end
25
26 @implementation CHGridView
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
27 @synthesize dataSource, centerTilesInGrid, allowsSelection, padding, preLoadMultiplier, rowHeight, perLine, sectionTitleHeight;
498b075 @camh first commit
authored Feb 22, 2010
28
9a1b26d @camh Fixed a couple bugs. Will be blank if no data source is set instead o…
authored Feb 22, 2010
29 - (id)init{
30 return [self initWithFrame:CGRectZero];
31 }
32
498b075 @camh first commit
authored Feb 22, 2010
33 - (id)initWithFrame:(CGRect)frame{
34 if(self = [super initWithFrame:frame]){
35 if(visibleTiles == nil)
36 visibleTiles = [[NSMutableArray alloc] init];
37
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
38 if(visibleSectionHeaders == nil)
39 visibleSectionHeaders = [[NSMutableArray alloc] init];
498b075 @camh first commit
authored Feb 22, 2010
40
41 if(reusableTiles == nil)
42 reusableTiles = [[NSMutableArray alloc] init];
43
44 if(layout == nil)
45 layout = [[CHGridLayout alloc] init];
46
10c7212 @camh preLoadMultiplier is now set to 0.5 for iPhone 3G, Original iPhone an…
authored Feb 22, 2010
47 if(sectionCounts == nil)
48 sectionCounts = [[NSMutableArray alloc] init];
49
498b075 @camh first commit
authored Feb 22, 2010
50 sections = 1;
51
52 allowsSelection = YES;
f2248c1 @camh Added a couple performance improvements
authored Mar 1, 2010
53 centerTilesInGrid = NO;
db6118e @camh Performance enhancements.
authored Feb 25, 2010
54 padding = CGSizeMake(10.0f, 10.0f);
55 rowHeight = 100.0f;
498b075 @camh first commit
authored Feb 22, 2010
56 perLine = 5;
db6118e @camh Performance enhancements.
authored Feb 25, 2010
57 sectionTitleHeight = 25.0f;
498b075 @camh first commit
authored Feb 22, 2010
58
54717ab @camh Deleted build directory
authored Mar 2, 2010
59 preLoadMultiplier = 6.0f;
10c7212 @camh preLoadMultiplier is now set to 0.5 for iPhone 3G, Original iPhone an…
authored Feb 23, 2010
60
9a1b26d @camh Fixed a couple bugs. Will be blank if no data source is set instead o…
authored Feb 22, 2010
61 [self setBackgroundColor:[UIColor whiteColor]];
db6118e @camh Performance enhancements.
authored Feb 25, 2010
62
63 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reuseHiddenTiles) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
498b075 @camh first commit
authored Feb 22, 2010
64 }
65 return self;
66 }
67
68 - (void)dealloc {
db6118e @camh Performance enhancements.
authored Feb 25, 2010
69 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
70
10c7212 @camh preLoadMultiplier is now set to 0.5 for iPhone 3G, Original iPhone an…
authored Feb 23, 2010
71 [sectionCounts release];
498b075 @camh first commit
authored Feb 22, 2010
72 [layout release];
73 [reusableTiles release];
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
74 [visibleSectionHeaders release];
498b075 @camh first commit
authored Feb 22, 2010
75 [visibleTiles release];
76 [super dealloc];
77 }
78
79 #pragma mark loading methods
80
81 - (void)loadVisibleSectionTitlesForSectionRange:(CHSectionRange)range{
82 CGRect b = self.bounds;
83 if(sections <= 1) return;
84
85 int i;
86 for (i = range.start; i <= range.end; i++) {
87 BOOL found = NO;
88
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
89 for(CHSectionHeaderView *header in visibleSectionHeaders){
90 if(header.section == i) found = YES;
498b075 @camh first commit
authored Feb 22, 2010
91 }
92
93 if(!found){
94 CGFloat yCoordinate = [layout yCoordinateForTitleOfSection:i];
95
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
96 CHSectionHeaderView *sectionHeader = nil;
498b075 @camh first commit
authored Feb 22, 2010
97
d6a7df4 @camh Uses UIScrollView delegate property now
authored Feb 24, 2010
98 if([[self delegate] respondsToSelector:@selector(titleViewForHeaderOfSection:inGridView:)]){
54717ab @camh Deleted build directory
authored Mar 2, 2010
99 sectionHeader = [[self delegate] headerViewForSection:i inGridView:self];
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
100 [sectionHeader setFrame:CGRectMake(b.origin.x, yCoordinate, b.size.width, sectionTitleHeight)];
498b075 @camh first commit
authored Feb 22, 2010
101 }else{
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
102 sectionHeader = [[CHSectionHeaderView alloc] initWithFrame:CGRectMake(b.origin.x, yCoordinate, b.size.width, sectionTitleHeight)];
498b075 @camh first commit
authored Feb 22, 2010
103 if([dataSource respondsToSelector:@selector(titleForHeaderOfSection:inGridView:)])
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
104 [sectionHeader setTitle:[dataSource titleForHeaderOfSection:i inGridView:self]];
498b075 @camh first commit
authored Feb 22, 2010
105 }
106
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
107 [sectionHeader setYCoordinate:yCoordinate];
108 [sectionHeader setSection:i];
109 [sectionHeader setAutoresizingMask:(UIViewAutoresizingFlexibleWidth)];
498b075 @camh first commit
authored Feb 22, 2010
110
db6118e @camh Performance enhancements.
authored Feb 25, 2010
111 if(self.dragging || self.decelerating)
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
112 [self insertSubview:sectionHeader atIndex:self.subviews.count - 1];
498b075 @camh first commit
authored Feb 22, 2010
113 else
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
114 [self insertSubview:sectionHeader atIndex:self.subviews.count];
498b075 @camh first commit
authored Feb 22, 2010
115
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
116 [visibleSectionHeaders addObject:sectionHeader];
117 [sectionHeader release];
498b075 @camh first commit
authored Feb 22, 2010
118 }
119 }
120
121 [self removeSectionTitleNotInRange:range];
122 }
123
db6118e @camh Performance enhancements.
authored Feb 25, 2010
124 - (void)loadVisibleTileForIndexPath:(CHGridIndexPath)indexPath withRect:(CGRect)r{
498b075 @camh first commit
authored Feb 22, 2010
125 for(CHTileView *tile in visibleTiles){
db6118e @camh Performance enhancements.
authored Feb 25, 2010
126 CHGridIndexPath tileIndex = tile.indexPath;
127 if(tileIndex.section == indexPath.section && tileIndex.tileIndex == indexPath.tileIndex){
498b075 @camh first commit
authored Feb 22, 2010
128 return;
129 }
130 }
131
132 CHTileView *tile = [dataSource tileForIndexPath:indexPath inGridView:self];
133
db6118e @camh Performance enhancements.
authored Feb 25, 2010
134 [tile setIndexPath:indexPath];
135 [tile setSelected:NO];
136
f2248c1 @camh Added a couple performance improvements
authored Mar 1, 2010
137 if([[self delegate] respondsToSelector:@selector(sizeForTileAtIndex:inGridView:)] && centerTilesInGrid){
d6a7df4 @camh Uses UIScrollView delegate property now
authored Feb 24, 2010
138 CGSize size = [[self delegate] sizeForTileAtIndex:indexPath inGridView:self];
db6118e @camh Performance enhancements.
authored Feb 25, 2010
139 CGRect centeredRect = [layout centerRect:CGRectMake(0.0f, 0.0f, size.width, size.height) inLargerRect:r roundUp:NO];
140 centeredRect.origin.y += r.origin.y;
141 centeredRect.origin.x += r.origin.x;
498b075 @camh first commit
authored Feb 22, 2010
142 [tile setFrame:centeredRect];
2e80c43 @camh Couple fixes, README change
authored Feb 22, 2010
143 [tile setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin)];
498b075 @camh first commit
authored Feb 22, 2010
144 }else{
db6118e @camh Performance enhancements.
authored Feb 25, 2010
145 [tile setFrame:r];
498b075 @camh first commit
authored Feb 22, 2010
146 [tile setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth)];
147 }
148
f2248c1 @camh Added a couple performance improvements
authored Mar 1, 2010
149 [tile setBackgroundColor:self.backgroundColor];
150
db6118e @camh Performance enhancements.
authored Feb 25, 2010
151 [self insertSubview:tile atIndex:0];
498b075 @camh first commit
authored Feb 22, 2010
152 [visibleTiles addObject:tile];
153 }
154
61e3b0e @camh Improved reusable cells converter and changed README
authored Feb 22, 2010
155 - (void)reuseHiddenTiles{
f2248c1 @camh Added a couple performance improvements
authored Mar 1, 2010
156 NSMutableArray *toReuse = [[NSMutableArray alloc] init];
498b075 @camh first commit
authored Feb 22, 2010
157
61e3b0e @camh Improved reusable cells converter and changed README
authored Feb 22, 2010
158 CGRect b = self.bounds;
159 CGFloat contentOffsetY = self.contentOffset.y;
db6118e @camh Performance enhancements.
authored Feb 25, 2010
160 float pixelMargin = rowHeight * ([layout preLoadMultiplier]);
161
162 CGFloat firstY = (b.size.height + contentOffsetY + pixelMargin);
163 CGFloat secondY = contentOffsetY - pixelMargin;
61e3b0e @camh Improved reusable cells converter and changed README
authored Feb 22, 2010
164
498b075 @camh first commit
authored Feb 22, 2010
165 for(CHTileView *tile in visibleTiles){
db6118e @camh Performance enhancements.
authored Feb 25, 2010
166 CGRect r = tile.frame;
167 if(r.origin.y > firstY || r.origin.y + r.size.height < secondY){
498b075 @camh first commit
authored Feb 22, 2010
168 [toReuse addObject:tile];
1d1d2cb @camh A couple performance enhancements
authored Feb 22, 2010
169 if(reusableTiles.count < maxReusable) [reusableTiles addObject:tile];
498b075 @camh first commit
authored Feb 22, 2010
170 }
171 }
172
1d1d2cb @camh A couple performance enhancements
authored Feb 23, 2010
173 [visibleTiles removeObjectsInArray:toReuse];
f2248c1 @camh Added a couple performance improvements
authored Mar 1, 2010
174 [toReuse release];
498b075 @camh first commit
authored Feb 22, 2010
175 }
176
177 - (void)removeSectionTitleNotInRange:(CHSectionRange)range{
db6118e @camh Performance enhancements.
authored Feb 25, 2010
178 NSMutableArray *toDelete = [NSMutableArray array];
498b075 @camh first commit
authored Feb 22, 2010
179
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
180 for (CHSectionHeaderView *header in visibleSectionHeaders) {
181 int s = header.section;
db6118e @camh Performance enhancements.
authored Feb 25, 2010
182 if(s < range.start || s > range.end){
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
183 [toDelete addObject:header];
498b075 @camh first commit
authored Feb 22, 2010
184 }
185 }
186
1d1d2cb @camh A couple performance enhancements
authored Feb 23, 2010
187 [toDelete makeObjectsPerformSelector:@selector(removeFromSuperview)];
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
188 [visibleSectionHeaders removeObjectsInArray:toDelete];
498b075 @camh first commit
authored Feb 22, 2010
189 }
190
191 - (void)reloadData{
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
192 [self reloadDataAndLayoutUpdateNeeded:YES];
193 }
194
195 - (void)reloadDataAndLayoutUpdateNeeded:(BOOL)layoutNeeded{
9a1b26d @camh Fixed a couple bugs. Will be blank if no data source is set instead o…
authored Feb 22, 2010
196 if(dataSource == nil) return;
197
498b075 @camh first commit
authored Feb 22, 2010
198 [self removeAllSubviews];
199 [visibleTiles removeAllObjects];
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
200 [visibleSectionHeaders removeAllObjects];
201
498b075 @camh first commit
authored Feb 22, 2010
202 CGRect b = [self bounds];
203
2e80c43 @camh Couple fixes, README change
authored Feb 23, 2010
204 if([dataSource respondsToSelector:@selector(numberOfSectionsInGridView:)]){
205 sections = [dataSource numberOfSectionsInGridView:self];
206 if(sections == 0) sections = 1;
207 }else {
208 sections = 1;
209 }
210
10c7212 @camh preLoadMultiplier is now set to 0.5 for iPhone 3G, Original iPhone an…
authored Feb 23, 2010
211 [sectionCounts removeAllObjects];
212
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
213 if(layoutNeeded){
214 [layout setGridWidth:b.size.width];
215 [layout setPadding:padding];
216 [layout setPerLine:perLine];
217 [layout setPreLoadMultiplier:preLoadMultiplier];
218 [layout setRowHeight:rowHeight];
219 [layout setSectionTitleHeight:sectionTitleHeight];
220
221 [layout setSections:sections];
222 int i;
223 for(i = 0; i < sections; i++){
224 int numberInSection = [dataSource numberOfTilesInSection:i GridView:self];
225 [sectionCounts addObject:[NSNumber numberWithInt:numberInSection]];
226 [layout setNumberOfTiles:numberInSection ForSectionIndex:i];
227 }
498b075 @camh first commit
authored Feb 22, 2010
228
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
229 [layout updateLayout];
498b075 @camh first commit
authored Feb 22, 2010
230 }
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
231
498b075 @camh first commit
authored Feb 22, 2010
232 [self setNeedsLayout];
db6118e @camh Performance enhancements.
authored Feb 25, 2010
233
234 maxReusable = ceilf((self.bounds.size.height / rowHeight) * perLine) * 2;
f2248c1 @camh Added a couple performance improvements
authored Mar 1, 2010
235
236 if([layout contentHeight] > b.size.height)
237 [self setContentSize:CGSizeMake(b.size.width, [layout contentHeight])];
238 else
239 [self setContentSize:CGSizeMake(b.size.width, b.size.height + 1.0)];
498b075 @camh first commit
authored Feb 22, 2010
240 }
241
242 - (CHTileView *)dequeueReusableTileWithIdentifier:(NSString *)identifier{
54717ab @camh Deleted build directory
authored Mar 2, 2010
243 CHTileView *foundTile = nil;
244 BOOL found = NO;
245
498b075 @camh first commit
authored Feb 22, 2010
246 for(CHTileView *tile in reusableTiles){
54717ab @camh Deleted build directory
authored Mar 2, 2010
247 if(!found && [[tile reuseIdentifier] isEqualToString:identifier]){
248 foundTile = tile;
249 found = YES;
db6118e @camh Performance enhancements.
authored Feb 25, 2010
250 }
498b075 @camh first commit
authored Feb 22, 2010
251 }
54717ab @camh Deleted build directory
authored Mar 2, 2010
252
253 if(foundTile){
254 [[foundTile retain] autorelease];
255 [reusableTiles removeObject:foundTile];
256 }
257
258 return foundTile;
498b075 @camh first commit
authored Feb 22, 2010
259 }
260
261 #pragma mark view and layout methods
262
263 - (void)layoutSubviews{
9a1b26d @camh Fixed a couple bugs. Will be blank if no data source is set instead o…
authored Feb 22, 2010
264 if(dataSource == nil) return;
265
498b075 @camh first commit
authored Feb 22, 2010
266 CGRect b = [self bounds];
267
db6118e @camh Performance enhancements.
authored Feb 25, 2010
268 [self reuseHiddenTiles];
269
270 CGFloat contentOffsetY = self.contentOffset.y;
271 CGFloat pixelMargin = rowHeight * [layout preLoadMultiplier];
272 CGFloat firstY = (b.size.height + contentOffsetY + pixelMargin);
273 CGFloat secondY = contentOffsetY - pixelMargin;
f2248c1 @camh Added a couple performance improvements
authored Mar 1, 2010
274
275 BOOL hasSections = (sections > 1);
276
277 if(hasSections){
278 CHSectionRange sectionRange = [layout sectionRangeForContentOffset:contentOffsetY andHeight:b.size.height];
279 [self loadVisibleSectionTitlesForSectionRange:sectionRange];
280 [self calculateSectionTitleOffset];
281
282 }
db6118e @camh Performance enhancements.
authored Feb 25, 2010
283
284 for(CHGridLayoutTile *tile in [layout justTiles]){
285 CGRect r = [tile rect];
286 if(r.origin.y < firstY && r.origin.y + r.size.height > secondY){
287 [self loadVisibleTileForIndexPath:tile.indexPath withRect:r];
288 }
289 }
498b075 @camh first commit
authored Feb 22, 2010
290
db6118e @camh Performance enhancements.
authored Feb 25, 2010
291 //if([[self delegate] respondsToSelector:@selector(visibleTilesChangedTo:)]) [[self delegate] visibleTilesChangedTo:visibleTiles.count];
498b075 @camh first commit
authored Feb 22, 2010
292 }
293
294 - (void)removeAllSubviews{
295 [visibleTiles makeObjectsPerformSelector:@selector(removeFromSuperview)];
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
296 [visibleSectionHeaders makeObjectsPerformSelector:@selector(removeFromSuperview)];
f2248c1 @camh Added a couple performance improvements
authored Mar 1, 2010
297 [reusableTiles makeObjectsPerformSelector:@selector(removeFromSuperview)];
498b075 @camh first commit
authored Feb 22, 2010
298 }
299
300 #pragma mark tiles accessor methods
301
302 - (CHTileView *)tileForIndexPath:(CHGridIndexPath)indexPath{
303 CHTileView *foundTile = nil;
304
305 for(CHTileView *tile in visibleTiles){
306 if(tile.indexPath.section == indexPath.section && tile.indexPath.tileIndex == indexPath.tileIndex)
307 foundTile = tile;
308 }
309
310 return foundTile;
311 }
312
313 - (NSMutableArray *)tilesForSection:(int)section{
314 NSMutableArray *array = [NSMutableArray array];
315
316 for(CHTileView *tile in visibleTiles){
317 if(tile.indexPath.section == section){
318 [array addObject:tile];
319 }
320 }
321
322 if(array.count > 0) return array;
323 return nil;
324 }
325
326 - (NSMutableArray *)tilesFromIndex:(int)startIndex toIndex:(int)endIndex inSection:(int)section{
327 NSMutableArray *array = [NSMutableArray array];
328
329 for(CHTileView *tile in visibleTiles){
330 if(tile.indexPath.section == section && tile.indexPath.tileIndex >= startIndex && tile.indexPath.tileIndex <= endIndex){
331 [array addObject:tile];
332 }
333 }
334
335 if(array.count > 0) return array;
336 return nil;
337 }
338
339 #pragma mark section title accessor methods
340
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
341 - (CHSectionHeaderView *)sectionHeaderViewForSection:(int)section{
342 CHSectionHeaderView *headerView = nil;
498b075 @camh first commit
authored Feb 22, 2010
343
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
344 for(CHSectionHeaderView *header in visibleSectionHeaders){
6bc4255 @camh Fixed a bug in sectionHeaderViewForSection, thanks to Pierre-Arnaud M…
authored Mar 2, 2010
345 if([header section] == section) headerView = header;
498b075 @camh first commit
authored Feb 22, 2010
346 }
347
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
348 return headerView;
498b075 @camh first commit
authored Feb 22, 2010
349 }
350
351 #pragma mark indexPath accessor methods
352
353 - (CHGridIndexPath)indexPathForPoint:(CGPoint)point{
354 return CHGridIndexPathMake(0, 0);
355 }
356
e459685 @camh Added support to scroll to a tile at a specific index path.
authored Feb 23, 2010
357 #pragma mark tile scrolling methods
358
359 - (void)scrollToTileAtIndexPath:(CHGridIndexPath)indexPath animated:(BOOL)animated{
360 CGRect r = [layout tileFrameForIndexPath:indexPath];
361 [self scrollRectToVisible:r animated:animated];
362 }
363
498b075 @camh first commit
authored Feb 22, 2010
364 #pragma mark selection methods
365
366 - (void)deselectTileAtIndexPath:(CHGridIndexPath)indexPath{
367 for(CHTileView *tile in visibleTiles){
368 if(tile.indexPath.section == indexPath.section && tile.indexPath.tileIndex == indexPath.tileIndex){
369 [tile setSelected:NO];
370 selectedTile = nil;
371 }
372 }
373 }
374
f2248c1 @camh Added a couple performance improvements
authored Mar 1, 2010
375 - (void)deselectSelectedTile{
498b075 @camh first commit
authored Feb 22, 2010
376 if(selectedTile){
377 [self deselectTileAtIndexPath:selectedTile.indexPath];
378 selectedTile = nil;
379 }
380 }
381
d6a7df4 @camh Uses UIScrollView delegate property now
authored Feb 24, 2010
382 #pragma mark property setters and getters
498b075 @camh first commit
authored Feb 22, 2010
383
d6a7df4 @camh Uses UIScrollView delegate property now
authored Feb 24, 2010
384 - (id<CHGridViewDelegate>)delegate {
385 return (id<CHGridViewDelegate>)[super delegate];
498b075 @camh first commit
authored Feb 22, 2010
386 }
387
d6a7df4 @camh Uses UIScrollView delegate property now
authored Feb 24, 2010
388 - (void)setDelegate:(id<UIScrollViewDelegate,CHGridViewDelegate>)d{
389 [super setDelegate:d];
390 }
391
392 - (void)setDataSource:(id<CHGridViewDataSource>)d{
393 dataSource = d;
498b075 @camh first commit
authored Feb 22, 2010
394 }
395
f2248c1 @camh Added a couple performance improvements
authored Mar 1, 2010
396 - (void)setCenterTilesInGrid:(BOOL)b{
397 centerTilesInGrid = b;
498b075 @camh first commit
authored Feb 22, 2010
398 [self setNeedsLayout];
399 }
400
401 - (void)setAllowsSelection:(BOOL)allows{
402 allowsSelection = allows;
403 }
404
405 #pragma mark touch methods
406
407 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
408 [super touchesBegan:touches withEvent:event];
409 UITouch *touch = [[event allTouches] anyObject];
410 CGPoint location = [touch locationInView:self];
411
412 UIView *view = [self hitTest:location withEvent:event];
413
414 if([view isKindOfClass:[CHTileView class]] && allowsSelection){
415 if(selectedTile)
416 [self deselectTileAtIndexPath:selectedTile.indexPath];
417
418 selectedTile = (CHTileView *)view;
419 [selectedTile setSelected:YES];
420 }
421 }
422
423 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
424 [super touchesMoved:touches withEvent:event];
425
426 if(self.dragging || self.tracking || self.decelerating && allowsSelection){
427 if(selectedTile != nil){
428 [selectedTile setSelected:NO];
429 selectedTile = nil;
430 }
431 }
432 }
433
434 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
435 [super touchesEnded:touches withEvent:event];
436 UITouch *touch = [[event allTouches] anyObject];
437 CGPoint location = [touch locationInView:self];
438
439 UIView *view = [self hitTest:location withEvent:event];
440
441 if(selectedTile != nil && [selectedTile isEqual:view] && allowsSelection){
d6a7df4 @camh Uses UIScrollView delegate property now
authored Feb 24, 2010
442 if([[self delegate] respondsToSelector:@selector(selectedTileAtIndexPath:inGridView:)])
443 [[self delegate] selectedTileAtIndexPath:[selectedTile indexPath] inGridView:self];
498b075 @camh first commit
authored Feb 22, 2010
444 }
445 }
446
447 #pragma mark section title view offset
448
449 - (void)calculateSectionTitleOffset{
450 float offset = self.contentOffset.y;
451
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
452 for(CHSectionHeaderView *header in visibleSectionHeaders){
453 CGRect f = [header frame];
454 float sectionY = [header yCoordinate];
498b075 @camh first commit
authored Feb 22, 2010
455
db6118e @camh Performance enhancements.
authored Feb 25, 2010
456 if(sectionY <= offset && offset > 0.0f){
498b075 @camh first commit
authored Feb 22, 2010
457 f.origin.y = offset;
db6118e @camh Performance enhancements.
authored Feb 25, 2010
458 if(offset <= 0.0f) f.origin.y = sectionY;
498b075 @camh first commit
authored Feb 22, 2010
459
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
460 CHSectionHeaderView *sectionTwo = [self sectionHeaderViewForSection:header.section + 1];
498b075 @camh first commit
authored Feb 22, 2010
461 if(sectionTwo != nil){
db6118e @camh Performance enhancements.
authored Feb 25, 2010
462 CGFloat sectionTwoHeight = sectionTwo.frame.size.height;
463 CGFloat sectionTwoY = sectionTwo.yCoordinate;
464 if((offset + sectionTwoHeight) >= sectionTwoY){
465 f.origin.y = sectionTwoY - sectionTwoHeight;
498b075 @camh first commit
authored Feb 22, 2010
466 }
467 }
468 }else{
db6118e @camh Performance enhancements.
authored Feb 25, 2010
469 f.origin.y = sectionY;
498b075 @camh first commit
authored Feb 22, 2010
470 }
471
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
472 if(f.origin.y <= offset) [header setOpaque:NO];
473 else [header setOpaque:YES];
498b075 @camh first commit
authored Feb 22, 2010
474
d2ec268 @camh Changed CHSectionTitleView to CHSectionHeaderView
authored Mar 2, 2010
475 [header setFrame:f];
498b075 @camh first commit
authored Feb 22, 2010
476 }
477 }
478
479 @end
Something went wrong with that request. Please try again.