#import "ViewController.h"
@interface ViewController ()
@property (
weak,
nonatomic)
IBOutlet UICollectionViewFlowLayout *flowLayout;
@end
static NSString *ID = @
"cell";
static NSString *HeaderID = @
"header";
static NSString *FooterID = @
"footer";
@implementation ViewController
- (
void)viewDidLoad {
[
super viewDidLoad];
[
self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
self.collectionView.backgroundColor = [
UIColor whiteColor];
[
self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderID];
self.flowLayout.headerReferenceSize = CGSizeMake(
50,
50);
[
self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FooterID];
self.flowLayout.footerReferenceSize = CGSizeMake(
50,
50);
self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
if ([UIDevice currentDevice]
.systemVersion.floatValue >=
9.0) {
self.flowLayout.sectionHeadersPinToVisibleBounds =
YES;
self.flowLayout.sectionFootersPinToVisibleBounds =
YES;
}
}
- (
NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 5;
}
- (
NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(
NSInteger)section {
return 15;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(
NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
cell
.backgroundColor = [
UIColor colorWithRed:((
float)arc4random_uniform(
256) /
255.0) green:((
float)arc4random_uniform(
256) /
255.0) blue:((
float)arc4random_uniform(
256) /
255.0) alpha:
1.0];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(
NSString *)kind atIndexPath:(
NSIndexPath *)indexPath {
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HeaderID forIndexPath:indexPath];
headerView
.backgroundColor = [
UIColor greenColor];
return headerView;
}
else {
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FooterID forIndexPath:indexPath];
footerView
.backgroundColor = [
UIColor purpleColor];
return footerView;
}
}
@end
转载请注明原文地址: https://ju.6miu.com/read-1308113.html