ios小白一枚,刚开始学习,所以对于很多理论上的东西研究的还不够,写文章仅仅是为了养成一个好的习惯,有问题可以直接指出,虚心接受,请勿喷….
一、简单介绍UICollectionView:
UICollectionView是ios6.0之后才出来的一种控件,是对UITableView一种更好的用户(程序猿)体验。基于UICollectionView 我们可以实现更炫的效果。 需要支持3种协议:
(UICollectionViewDateSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout)前两种参考UITableView就可以了。后面一种是对UICollectionView的布局.不同于UITableView的,UICollectionViewCell必须提前注册,这样在缓存池里面找不到cell后就会直接用注册过的创建一个cell.下面介绍一下cell的两种注册方法,以及对应的一些重要的代理方法
1. 两种注册方法:
1.1 class注册:
[self.collectionView registerClass:[ZHCollectionViewCell class] forCellWithReuseIdentifier:@"ZHCollectionCell"];
1.2 Nib注册
//先获取一个nib对象
UINib *nib = [UINib nibWithNibName:@"ZHCollectionReusableView" bundle:[NSBundle mainBundle]];
[self.collectionView registerNib:nib forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
1.2 关键的代理方法
#pragma mark - UICollectionViewDateSource 数据源方法
//返回这个section个数据个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
//cell的创建
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
//返回footer/headerView
-dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
#pragma mark - ZHCollectionReusableViewdelegate header/footer代理方法
- (void)collectionReusableViewedidapplist: