iOS控件--UICollectionView--集合视图控件

UICollectionView多用于在App中对瀑布流的使用和处理,可加载左右或者上下滑动的界面,展示集合视图实现多列布局,用法就是类似于UItableView和UITableViewController类。

其中使用 UICollectionView必须实现 UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewFlowLayout这三个协议。

UICollectionView -- Delegate/DataSource 常用方法的介绍:

// 设定视图分组数 -- 不能设置为0
 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
// 设定每组集合个数
 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
 
// each `UICollectionViewCell` size.
 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;  

/// 下面的两个设置布局距离和 UICollectionView 创建时布局方向的 竖直和水平有关系,默认竖直布局。
// horizontal distance. 
 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section; 
// vertical distance.
 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section; 

// secotion距离头视图的间距
 -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
 
// UICollectionView cell content show or cell action operate.
// 注意此处的 `cellForItem` 方法,如果出现不被调用的情况可能是 `numberOfSections...`/`numberOfItems...` 返回值有问题;或者是设置的delegate/dataSource 有问题。
 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
     CustomCollectionViewCell *cell = (MyWorkCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kDefaultCellIdentifier forIndexPath:indexPath]; // kDefaultCellIdentifier is equal to top.
 }
 
 // 每个cell被点击后的触发方法
 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;

 //返回这个 `UICollectionView`某个cell 是否可以被选择
 -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
 }

对于 UICollectionViewinit 时也有很深层次的讲究.

static NSString * const kDefaultCellIdentifier = @"collectionViewCellIdentifier";

// 可直接拖入自定义的分类使用的自定义 UICollectionView
+ (UICollectionView *)InitCVWithBGColor:(UIColor *)BGColor CVFrame:(CGRect)CVFrame withNeedHeaderFooter:(BOOL)isNeed withHSize:(CGSize)HSize withFSize:(CGSize)FSize withHID:(NSString *)HID withFID:(NSString *)FID withRegisterNib:(NSString *)NibName withCellId:(NSString *)cellId withDelegateVC:(id)delegateVC
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 自定义的CVFrame
    UICollectionView *currentCV = [[UICollectionView alloc]initWithFrame:CVFrame collectionViewLayout:layout];
    currentCV.backgroundColor = BGColor;
    // 判断是否需要有 头视图/尾部视图的情况,针对每个cell来说
    if(isNeed){
        layout.headerReferenceSize = HSize;
        layout.footerReferenceSize = FSize;
        [currentCV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HID];
        [currentCV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FID];
    }
    
    // cellId是必须有的,就是下面的这句代码必须执行某则会奔溃 (我这里使用的是 `registerNib` 方法即需要自定义nib文件来加载)
    if (cellId == nil || cellId.length == 0) {
        [currentCV registerNib:[UINib nibWithNibName:NibName bundle:nil] forCellWithReuseIdentifier:kDefaultCellIdentifier];
    }else{
        [currentCV registerNib:[UINib nibWithNibName:NibName bundle:nil] forCellWithReuseIdentifier:cellId];
    }
    
    // 或者直接使用 `registerClass`注册cellId也是可以的
    //[currentCV registerClass:[CollectionCell class] forCellWithReuseIdentifier:cellId];  
    
    
    currentCV.delegate = delegateVC;
    currentCV.dataSource = delegateVC;
    currentCV.bounces = YES;
}

对于 UICollectionView 方法的init必须按照上面的步骤方法走,除了可否定的 透视图/尾视图外,缺少哪一步骤外可能会导致程序奔溃,或者需要的视图未显示出来。

以上是对 UICollectionView 控件的使用总结,不以偏概全,只授之以渔,有更好的操作也会及时更新如果您有UICollectionView控件的更好使用欢迎留言交流!
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容