forked from soffes/sstoolkit
/
SCCollectionViewDemoViewController.m
100 lines (70 loc) · 3.1 KB
/
SCCollectionViewDemoViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// SCCollectionViewDemoViewController.m
// SSCatalog
//
// Created by Sam Soffes on 8/24/10.
// Copyright 2010 Sam Soffes. All rights reserved.
//
#import "SCCollectionViewDemoViewController.h"
#import "SCImageCollectionViewItem.h"
@implementation SCCollectionViewDemoViewController
#pragma mark - Class Methods
+ (NSString *)title {
return @"Collection View";
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = [[self class] title];
self.collectionView.extremitiesStyle = SSCollectionViewExtremitiesStyleScrolling;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
return YES;
}
#pragma mark - SSCollectionViewDataSource
- (NSUInteger)numberOfSectionsInCollectionView:(SSCollectionView *)aCollectionView {
return 10;
}
- (NSUInteger)collectionView:(SSCollectionView *)aCollectionView numberOfItemsInSection:(NSUInteger)section {
return 50;
}
- (SSCollectionViewItem *)collectionView:(SSCollectionView *)aCollectionView itemForIndexPath:(NSIndexPath *)indexPath {
static NSString *const itemIdentifier = @"itemIdentifier";
SCImageCollectionViewItem *item = (SCImageCollectionViewItem *)[aCollectionView dequeueReusableItemWithIdentifier:itemIdentifier];
if (item == nil) {
item = [[[SCImageCollectionViewItem alloc] initWithReuseIdentifier:itemIdentifier] autorelease];
}
CGFloat size = 80.0f * [[UIScreen mainScreen] scale];
NSInteger i = (50 * indexPath.section) + indexPath.row;
item.imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.gravatar.com/avatar/%i?s=%0.f&d=identicon", i, size]];
return item;
}
- (UIView *)collectionView:(SSCollectionView *)aCollectionView viewForHeaderInSection:(NSUInteger)section {
SSLabel *header = [[SSLabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 40.0f)];
header.autoresizingMask = UIViewAutoresizingFlexibleWidth;
header.text = [NSString stringWithFormat:@"Section %i", section + 1];
header.textEdgeInsets = UIEdgeInsetsMake(0.0f, 19.0f, 0.0f, 19.0f);
header.shadowColor = [UIColor whiteColor];
header.shadowOffset = CGSizeMake(0.0f, 1.0f);
header.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.8f];
return [header autorelease];
}
#pragma mark - SSCollectionViewDelegate
- (CGSize)collectionView:(SSCollectionView *)aCollectionView itemSizeForSection:(NSUInteger)section {
return CGSizeMake(80.0f, 80.0f);
}
- (void)collectionView:(SSCollectionView *)aCollectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *title = [NSString stringWithFormat:@"You selected item %i in section %i!",
indexPath.row + 1, indexPath.section + 1];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:nil delegate:nil
cancelButtonTitle:@"Oh, awesome!" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (CGFloat)collectionView:(SSCollectionView *)aCollectionView heightForHeaderInSection:(NSUInteger)section {
return 40.0f;
}
@end