UI整理-----part5--UICollectionView

(1)可以理解成多列的UITableView(这是UICollectionView的最简单的形式)

(2)实现一个UICollectionView和实现一个UITableView基本没什么大区别,它们同样都是由dataSource和delegate设计模式的:dataSource为View提供数据源,delegate提供一些样式的小细节以及用户交互响应

(3)保证CollectionView正常工作的三个方法:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView        section的数量

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section          某个section里有多少item

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath              对于某个位置应该显示什么样的cell

(4)UICollectionViewCell的布局是由UICollectionViewLayout类的子类 UICollectionViewFlowLayout 决定的:

{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    layout.itemSize = CGSizeMake(100, 100);

    layout.minimumInteritemSpacing = 20;  //设置单元格水平间隔,默认为10

    layout.minimumLineSpacing = 20;      //设置行间距为20

    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);   //设置与边框距离

    layout.scrollDirection = UICollectionViewScrollDirectionVertical;  //设置滚动方向

    layout.headerReferenceSize = CGSizeMake(0, 200);     //设置头部视图大小只需要设置height 

}

(5)在创建完布局类之后,需要实现实例化一个UICollectionView

    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];

(6)通过注册的方式创建单元格头视图    [collectionViewregisterClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];     其中:UICollectionReusableView——collectionView共用的类   UICollectionElementKindSectionHeader——注册头视图的类型

(7)触摸事件:- (void)collectionView:(UICollectionView *)collectionViewdidSelectItemAtIndexPath:(NSIndexPath *)indexPath

原文地址:https://www.cnblogs.com/8023huihui/p/5203983.html