#pragma mark - 设置自定义视图
- (void)loadView
{
self.rootView = [[[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
_rootView.backgroundColor = [UIColor redColor];
self.view = _rootView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 设置数据源和代理
self.rootView.collectionView.dataSource = self;
self.rootView.collectionView.delegate = self;
// collectionViewCell只能使用storyBoard(xib)或者注册使用
[self.rootView.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
// 注册头区和尾区
[self.rootView.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
[self.rootView.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
}
#pragma mark - UICollectionViewDataSource Methods
#pragma mark 设置分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 3;
}
#pragma mark 设置每个分区的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 9;
}
#pragma mark 设置每个item上显示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 1. 直接去重用队列中查找,如果没有的话,会自动帮我们创建
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
// 2. 使用
cell.backgroundColor = [UIColor cyanColor];
cell.label.text = [NSString stringWithFormat:@"%ld %ld", indexPath.section, indexPath.row];
// 3. 返回
return cell;
}
#pragma mark 处理每个分区的头区和尾区
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
// 对于集合视图(UICollectionView)来说,头区和尾区使用的是UICollectionReusableView类,而不是UIView,设置的时候,也需要使用重用机制
// 使用kind去判断当前是显示头区还是尾区
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
// 头区
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor whiteColor];
return headerView;
} else {
// 尾区
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
footerView.backgroundColor = [UIColor redColor];
return footerView;
}
}
#pragma mark 处理item的点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"section:%ld row:%ld", indexPath.section, indexPath.row);
}
- (void)addAllViews
{
// 初始化集合视图
// 1. 创建布局对象
UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
//属性
// 设置每一个的大小
flowLayout.itemSize = CGSizeMake(110, 100);
// 设置左右的最小间距
flowLayout.minimumInteritemSpacing = 5;
// 设置上下的最小间距
flowLayout.minimumLineSpacing = 5;
// 滚动方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
// 设置头区大小
flowLayout.headerReferenceSize = CGSizeMake(self.bounds.size.width, 20);
// 设置尾区的大小
flowLayout.footerReferenceSize = CGSizeMake(self.bounds.size.width, 20);
// 设置边缘嵌入的值
flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
// 2. 使用布局,创建集合视图,
self.collectionView = [[[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout] autorelease];
[flowLayout release];
[self addSubview:_collectionView];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addAllViews];
//自定义集合视图cell设置圆角
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 15;
}
return self;
}
- (void)addAllViews
{
self.label = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 30)] autorelease];
_label.backgroundColor = [UIColor yellowColor];
_label.layer.masksToBounds = YES;
_label.layer.cornerRadius = 10;
[self.contentView addSubview:_label];
}