UICollectionView 与布局对象的关系

Relation between UICollectionView and layout object

我是 IOS 的新手,我想了解 UICollectionView 的概念。我正在阅读一些文章。所有文章都谈论布局对象。但我不明白 UICollectionView 和布局对象之间的关系。拜托,你能帮我理解这个概念吗?谢谢。

UICollectionViewLayout

The UICollectionViewLayout class is an abstract base class that you subclass and use to generate layout information for a collection view. The job of a layout object is to determine the placement of cells, supplementary views, and decoration views inside the collection view’s bounds and to report that information to the collection view when asked. The collection view then applies the provided layout information to the corresponding views so that they can be presented onscreen.

这是 UICollectionViewFlowLayout and UICollectionViewTransitionLayout 的基础 class。 初始化 UICollectionView 时,您必须指定要使用上述两种布局中的哪一种。如果您想要像 Instagram 或基本上任何 UITableView 这样的正常流式布局,那么您可以选择 UICollectionViewFlowLayout。如果您希望它具有专门的行为布局,例如创建交互式转换、手势识别器或触摸事件以在您的集合视图中从一种布局更改为另一种布局时实现行为,您可以 UICollectionViewTransitionLayout.

在代码中:

UICollectionViewFlowLayout *collectionLayout = [[UICollectionViewFlowLayout alloc] init];
// OR
UICollectionViewTransitionLayout *collectionLayout = [[UICollectionViewTransitionLayout alloc] init];

UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) collectionViewLayout:collectionLayout];
[myCollectionView setDelegate:self];
[myCollectionView setDataSource:self];
[myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
[self.view addSubview:myCollectionView];

注意: .. collectionViewLayout:collectionLayout];

Source