美文网首页iOS 开发好东西小知识点
【iOS 开发】UICollectionView 集合视图使用汇

【iOS 开发】UICollectionView 集合视图使用汇

作者: 爱吃鸭梨的猫 | 来源:发表于2017-06-30 17:54 被阅读153次
UICollectionView

UICollectionViewUITableView 的用法非常的相似,但它要更加强大,下面就对于 UICollectionView 的基本用法进行一个汇总,以便之后方便查看。


UICollectionView 的创建

/* 设置单元格布局 */
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(150, 120); // 单元格的尺寸
flow.minimumLineSpacing = 10.0; // 行之间间距
flow.minimumInteritemSpacing = 10.0; // 列之间的间距
flow.headerReferenceSize = CGSizeMake(100, 75); // 段头尺寸
flow.footerReferenceSize = CGSizeMake(100, 75); // 段尾尺寸
flow.scrollDirection = UICollectionViewScrollDirectionVertical; // 滑动方向

/* 创建集合视图 */
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];
collectionView.delegate = self; // 代理
collectionView.dataSource = self; // 数据源
collectionView.backgroundColor = [UIColor whiteColor]; // 背景色
collectionView.delaysContentTouches = NO; // 默认为 YES ,设为 NO 可以解决点击单元格后高亮效果显示延迟的问题
[self.view addSubview:collectionView];

/* 注册自定义单元格 */
[collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];

/* 注册自定义段头 */
[collectionView registerClass:[SectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];

/* 注册自定义段尾 */
[collectionView registerClass:[SectionFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];

使用纯代码自定义 UICollectionViewCell 单元格或者 UICollectionReusableView 段头段尾视图时,只需要重写 - (instancetype)initWithFrame:(CGRect)frame 方法即可。

UICollectionViewDataSource 数据源

1. 设置集合视图的段数

/**
 设置集合视图的段数

 @param collectionView 集合视图对象
 @return 段数
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 2;
}

2. 设置集合视图单元格数量

/**
 设置集合视图单元格数量

 @param collectionView 集合视图对象
 @param section 哪一段
 @return 单元格数量
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 12;
}

3. 设置集合视图内容

/**
 设置集合视图内容

 @param collectionView 集合视图对象
 @param indexPath 位置
 @return 单元格对象
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    /* 查找可复用的单元格,自定义需要注册 */
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    return cell;
}

4. 设置段头/段尾内容

/**
 设置段头/段尾内容

 @param collectionView 集合视图对象
 @param kind 类型
 @param indexPath 位置
 @return 段头/段尾视图
 */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        /* 查找可复用的段头视图,自定义需要注册 */
        SectionHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
        
        return header;
    }
    else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        
        /* 查找可复用的段尾视图,自定义需要注册 */
        SectionFooter *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Footer" forIndexPath:indexPath];
        
        return footer;
    }
    
    return nil;
}

UICollectionViewDelegate 代理方法

1. 设置单元格是否允许高亮

/**
 设置单元格是否允许高亮

 @param collectionView 单元格对象
 @param indexPath 位置
 @return 是否允许
 */
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}

2. 单元格处于高亮状态时触发

/**
 单元格处于高亮状态时触发

 @param collectionView 单元格对象
 @param indexPath 位置
 */
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    
    /* 得到当前选中单元格对象 */
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
}

3. 单元格取消高亮状态时触发

/**
 单元格取消高亮状态时触发
 
 @param collectionView 单元格对象
 @param indexPath 位置
 */
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    
    /* 得到当前选中单元格对象 */
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
}

4. 设置单元格是否允许点击

/**
 设置单元格是否允许点击

 @param collectionView 单元格对象
 @param indexPath 位置
 @return 是否允许
 */
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}

5. 设置单元格是否允许取消点击

/**
 设置单元格是否允许取消点击
 
 @param collectionView 单元格对象
 @param indexPath 位置
 @return 是否允许
 */
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}

6. 点击单元格触发

/**
 点击单元格触发

 @param collectionView 单元格对象
 @param indexPath 位置
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    /* 取消单元格当前选中状态 */
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    /* 得到当前选中单元格对象 */
    CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
}

7. 设置上下左右间隔

/**
 设置上下左右间隔

 @param collectionView 集合视图对象
 @param collectionViewLayout 单元格布局
 @param section 哪一段
 @return 间隔
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(5, 5, 5, 5); // 分别为上、左、下、右
}

还有一些不常用的暂时先不写,等用到的时候再补上,哪里有问题的话可以在下面留言。

将来的你,一定会感激现在拼命的自己,愿自己与读者的开发之路无限美好。

我的传送门: 博客简书微博GitHub

相关文章

网友评论

    本文标题:【iOS 开发】UICollectionView 集合视图使用汇

    本文链接:https://www.haomeiwen.com/subject/omqicxtx.html