UICollectionView是一个继承UIScrollView的类,用法与UITableView差不多,也可以自定义item来布局.下面介绍了UICollectionView的用法
- (void)addUICollectionView {
// 创建一个网状结构的布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 设置网状结构的具体属性
// 最小行间距(上下两行距离)
layout.minimumLineSpacing = 20;
// 最小列间距
layout.minimumInteritemSpacing = 20;
// item的大小
layout.itemSize = CGSizeMake(140, 200);
// 表头size(竖直滑只有高度影响, 水平滑只有宽度影响)
layout.headerReferenceSize = CGSizeMake(100, 100);
// 表尾size
layout.footerReferenceSize = CGSizeMake(100, 100);
// 滚动方向
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
// 内边距
layout.sectionInset = UIEdgeInsetsMake(30, 30, 30, 30);
// 设置item强制size
// layout.estimatedItemSize = CGSizeMake(10, 20);
// 创建collectionView
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
// 设置代理与数据源
collectionView.delegate = self;
collectionView.dataSource = self;
// 设置背景颜色 (默认黑色)
collectionView.backgroundColor = [UIColor grayColor];
// 显示视图
[self.view addSubview:collectionView];
[layout release];
[collectionView release];
// 注册cell
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
// 注册表头表尾
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeaderView"];
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFootView"];
}
#pragma mark -- 代理方法
// 返回每个分区有多少行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 10;
}
// 返回每个索引 下UICollectionViewCell的样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// 重用池中 有可以用的 就拿去用(返回给你)
// 如果没有 就创建一个
// 必须注册一下 要使用的标识符的cell 才能使用
// 系统才知道要从重用池中取出 哪个类型的cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor cyanColor];
return cell;
}
// 设置表头表尾 UICollectionReusableView(可复用的)
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
// 判断返回表头类型 还是 表尾类型
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
// 相当于去重用池里找 判断一下 有就用 没有就建
// UICollectionElementKindSectionHeader 表头的标识
// 可重用的view 分为两种 一种是表头 一种是表尾
UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeaderView" forIndexPath:indexPath];
// 设置颜色
headView.backgroundColor = [UIColor yellowColor];
return headView;
} else {
// 表尾
UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFootView" forIndexPath:indexPath];
// 设置颜色
footView.backgroundColor = [UIColor greenColor];
return footView;
}
}