项目遇到了集合视图,
感觉得整理下
又来一篇
遵守协议
@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
// 创建一个集合视图
- (void)addSubViews
{
// Item 布局 (网格状布局)
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 行边距
layout.minimumLineSpacing = 30;
// 列边距
layout.minimumInteritemSpacing = 30;
// 设置item的宽高
layout.itemSize = CGSizeMake(350, 450);
// 设置滑动方向(默认是上下滑动)
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
// 设置表头 只有高度影响表头
layout.headerReferenceSize = CGSizeMake(0, 200);
// 设置表尾 只有高度影响表尾
layout.footerReferenceSize = CGSizeMake(0, 100);
// 设置内边距
layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
// 初始化 集合视图
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout]; // 需要一个布局,先初始化一个
//设置代理
collectionView.dataSource = self;
collectionView.delegate = self;
// collectionView 默认是黑色
collectionView.backgroundColor = [UIColor cyanColor];
// 显示视图
[self.view addSubview:collectionView];
[collectionView release];
[layout release];
// 注册要用的cell
// Identifier 重用标示符 要一致
// <#(Class)#> 你的cell是那个类的 就添加那个类
// 使用系统的就注册系统的 如果自定义的话 就注册自定义的
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
// 注册表头
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader"];
// 注册表尾
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooter"];
}
#pragma mark -- dataSource --
// 返回分区数, 默认就1个分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
// 返回每个分区的Item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 返回每个Item的方法
// 必须有一步: 注册cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
// 使用UICollectionViewCell 一般都是自定义再使用 和tableView 一样 所有的自定义控件 都要加在contentView 上面
cell.contentView.backgroundColor = [UIColor grayColor];
return cell; // 不用释放
}
// 设置表头表尾 同代理方法 来实现
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
// 判断返回表头 还是表尾
// 因为参数是字符串的 ,判断是否相同不能用等号
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
// 返回表头 需要去复用集合中得到
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader" forIndexPath:indexPath];
// 加个颜色
headerView.backgroundColor = [UIColor greenColor];
return headerView;
}else{
// 返回表尾
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooter" forIndexPath:indexPath];
footerView.backgroundColor = [UIColor yellowColor];
return footerView;
}
}
over