免费开源的iOS开发学习平台

UICollectionView详解:8-UICollectionViewDelegateFlowLayout类的代理方法实现

UICollectionViewDelegateFlowLayout介绍

使用流式布局的集合视图中,其单元格的大小以及位置不仅可以通过属性设置,还可以通过UICollectionViewDelegateFlowLayout类中定义的协议方法进行动态设置。UICollectionViewDelegateFlowLayout协议中的所有方法都不是必须实现的,当没有实现该协议中的方法时,集合视图的布局样式会由其collectionViewLayout属性来设置。当需要实现该协议中的方法时,需要预先设置集合视图的delegate属性。

在UICollectionViewDelegateFlowLayout协议中,有如下几个常用的方法。

  • 设置单元格的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
  • 单元格的边距设置,即Cell整体相对于Header、Footer以及屏幕左右两侧的距离
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
  • 单元格之间横向间距与纵向间距的设置
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
  • 设置某个段Section的Header与Footer的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

示例代码

下方的示例代码中,通过UICollectionViewDelegateFlowLayout协议中提供的方法实现了对集合视图布局样式的定制。

  • 设置预置条件。设置集合视图的的布局类型为UICollectionViewFlowLayout,并且设置集合视图的代理对象以及代理对象遵守UICollectionViewDelegateFlowLayout协议。
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collectionView;
@end
- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}
  • 设置单元格Cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat screenWith = [UIScreen mainScreen].bounds.size.width;
    //每行4个Cell
    CGFloat cellWidth = (screenWith - 3 *20)/4;
    return CGSizeMake(cellWidth, 60);
}
  • 设置单元格的边距。单元格Cell的边距设置,即Cell整体相对于Header、Footer以及屏幕左右两侧的距离,优先级较高;
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets insets = UIEdgeInsetsMake(10, 20, 30, 40);
    return insets;
}
  • 设置单元格Cell之间的横向距离与纵向距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 20;
}
  • 设置段Seciton的header与footer的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
   CGSize headerSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 50);
    return headerSize;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    CGSize footerSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 50);
    return footerSize;
}

运行效果:

示例代码

https://github.com/99ios/8.3.8