UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
flowLayout.minimumLineSpacing =
10.0
;
flowLayout.minimumInteritemSpacing =
50.0
;
flowLayout.itemSize = CGSizeMake(
50
,
50
);
flowLayout.sectionInset = UIEdgeInsetsMake(
10
,
10
,
10
,
10
);
flowLayout.headerReferenceSize = CGSizeMake(
320
,
20
);
flowLayout.footerReferenceSize = CGSizeMake(
320
,
20
);
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(
0
,
0
,
320
,
568
) collectionViewLayout:flowLayout];
[flowLayout release];
collectionView.dataSource = self;
collectionView.delegate = self;
collectionView.backgroundColor = [UIColor orangeColor];
[collectionView registerClass:[CLCollectionViewCell
class
] forCellWithReuseIdentifier:cellIdentifier];
[collectionView registerClass:[HeadView
class
] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
[collectionView registerClass:[FootView
class
] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier];
[self.view addSubview:collectionView];
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return
10
;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return
10
;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CLCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor yellowColor];
cell.textLabel.text = [NSString stringWithFormat:@
"%ld"
, (
long
)indexPath.row];
return
cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if
(kind == UICollectionElementKindSectionHeader) {
HeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
headerView.textLabel.text = @
"让我组成头部!"
;
headerView.textLabel.textAlignment = NSTextAlignmentCenter;
headerView.textLabel.textColor = [UIColor whiteColor];
return
headerView;
}
FootView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerIdentifier forIndexPath:indexPath];
footerView.textLabel.text = @
"让我组成尾部!"
;
footerView.textLabel.textAlignment = NSTextAlignmentCenter;
footerView.textLabel.textColor = [UIColor whiteColor];
return
footerView;
}
#pragma mark - UICollectionViewDelegate
- (
void
)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@
"%d %d"
, indexPath.section, indexPath.row);
}
#pragma mark - UICollectionViewDelegateFlowLayout
[collectionView registerClass:[FootView
class
] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier];
[self.view addSubview:collectionView];