-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSLockedHeaderViewController.m
More file actions
176 lines (131 loc) · 6.48 KB
/
CSLockedHeaderViewController.m
File metadata and controls
176 lines (131 loc) · 6.48 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// CSLockedHeaderViewController.m
// CSStickyHeaderFlowLayoutDemo
//
// Created by James Tang on 11/2/14.
// Copyright (c) 2014 James Tang. All rights reserved.
//
#import "CSLockedHeaderViewController.h"
#import "CSCell.h"
#import "CSSectionHeader.h"
#import "CSStickyHeaderFlowLayout.h"
@interface CSLockedHeaderViewController ()
@property (nonatomic, strong) NSArray *sections;
@property (nonatomic, strong) UINib *headerNib;
@end
@implementation CSLockedHeaderViewController
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.sections = @[];
self.headerNib = [UINib nibWithNibName:@"CSSearchBarHeader" bundle:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self reloadLayout];
// Also insets the scroll indicator so it appears below the search bar
self.collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(44, 0, 0, 0);
[self.collectionView registerNib:self.headerNib
forSupplementaryViewOfKind:CSStickyHeaderParallaxHeader
withReuseIdentifier:@"header"];
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(add:)];
self.navigationItem.rightBarButtonItem = add;
[self add:nil];
}
- (void)add:(id)sender
{
// NSArray *new = @[
// @{@"Twitter":@"http://twitter.com"},
// @{@"Facebook":@"http://facebook.com"},
// @{@"Tumblr":@"http://tumblr.com"},
// @{@"Pinterest":@"http://pinterest.com"},
// @{@"Instagram":@"http://instagram.com"},
// @{@"Github":@"http://github.com"},
// @{@"Twitter":@"http://twitter.com"},
// @{@"Facebook":@"http://facebook.com"},
// @{@"Tumblr":@"http://tumblr.com"},
// @{@"Pinterest":@"http://pinterest.com"},
// @{@"Instagram":@"http://instagram.com"},
// @{@"Github":@"http://github.com"},
// ];
// self.sections = [self.sections arrayByAddingObjectsFromArray:new];
// [self.collectionView reloadData];
[self.collectionView performBatchUpdates:^{
NSArray *new = @[
@{@"Twitter":@"http://twitter.com"},
@{@"Facebook":@"http://facebook.com"},
@{@"Tumblr":@"http://tumblr.com"},
@{@"Pinterest":@"http://pinterest.com"},
@{@"Instagram":@"http://instagram.com"},
@{@"Github":@"http://github.com"},
@{@"Twitter":@"http://twitter.com"},
@{@"Facebook":@"http://facebook.com"},
@{@"Tumblr":@"http://tumblr.com"},
@{@"Pinterest":@"http://pinterest.com"},
@{@"Instagram":@"http://instagram.com"},
@{@"Github":@"http://github.com"},
];
self.sections = [self.sections arrayByAddingObjectsFromArray:new];
NSMutableIndexSet *set = [NSMutableIndexSet indexSet];
NSMutableArray *indexPaths = [NSMutableArray array];
int startIndex = 0;
[new enumerateObjectsUsingBlock:^(NSDictionary *item, NSUInteger idx, BOOL *stop) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:startIndex + idx];
[indexPaths addObject:indexPath];
[set addIndex:startIndex + idx];
}];
[self.collectionView insertSections:set];
[self.collectionView insertItemsAtIndexPaths:indexPaths];
} completion:nil];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self reloadLayout];
}
- (void)reloadLayout {
CSStickyHeaderFlowLayout *layout = (id)self.collectionViewLayout;
if ([layout isKindOfClass:[CSStickyHeaderFlowLayout class]]) {
layout.parallaxHeaderReferenceSize = CGSizeMake(self.view.frame.size.width, 44);
layout.itemSize = CGSizeMake(self.view.frame.size.width, layout.itemSize.height);
// Setting the minimum size equal to the reference size results
// in disabled parallax effect and pushes up while scrolls
layout.parallaxHeaderMinimumReferenceSize = CGSizeMake(self.view.frame.size.width, 44);
}
}
#pragma mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return [self.sections count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *obj = self.sections[indexPath.section];
CSCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"
forIndexPath:indexPath];
cell.textLabel.text = [[obj allValues] firstObject];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
NSDictionary *obj = self.sections[indexPath.section];
CSSectionHeader *cell = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:@"sectionHeader"
forIndexPath:indexPath];
cell.textLabel.text = [[obj allKeys] firstObject];
return cell;
} else if ([kind isEqualToString:CSStickyHeaderParallaxHeader]) {
UICollectionReusableView *cell = [collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:@"header"
forIndexPath:indexPath];
return cell;
}
return nil;
}
#pragma mark UICollectionViewDataSource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"hit test");
}
@end