和tableView非常类似,不过多了一个布局类
需要遵守的2个协议
<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
//网格布局类
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//设置网格视频的滑动方向
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//最低水平和垂直方向的间距(默认10,也可以通过代理设置)
layout.minimumInteritemSpacing = 50;
layout.minimumLineSpacing = 50;
//创建网格视图,并且设置布局对象
UICollectionView *cv = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
//collectionView中的cell和段头段尾view都需要通过注册方式
//注册段头kind使用:UICollectionElementKindSectionHeader
//注册段尾kind使用:UICollectionElementKindSectionFooter
1,必须实现的2个代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{//设置显示多少个cell
return 15;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{//设置每个cell的内容(必须提前注册)
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"qqq" forIndexPath:indexPath];
//config cell
return cell;
}
2,其它
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{//设置分段数量
return 1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{//cell大小,每行展示多少个cell,都依赖于cell的大小
return CGSizeMake(100, 170);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{//整个集合分段上左下右的边距
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{//最低水平方向间距-->layout.minimumInteritemSpacing = 40;
return 40;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{//最低垂直方向间距-->layout.minimumLineSpacing = 40;
return 40;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{//cell点击事件
NSLog(@"%d==%d",indexPath.section,indexPath.row);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{//设置段头view大小
return CGSizeMake(30, 30);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{//设置段尾view大小
return CGSizeMake(30, 30);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{//设置段头段尾view(必须提前注册了段头或段尾才能使用,类似cell)
MyView *mv = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"www" forIndexPath:indexPath];
return mv;
}