UI基础:UICollectionView

UITableView 和 UICollectionView的设计思路:

1.UITableView 的布局由TableView和UItableViewDelegate完成.

2.UICollectionView布局样式由UICollectionViewFlowLayout和UICollectionViewDelegate完成.

数据源:

1.UITableView由UITableViewDataSource提供.

2.UICollectionView由UICollectionViewDataSource提供.

布局样式:

1.UITableView是多列单行.

2.UICollectionView是多行多列.

1.UITableViewCell上自带imageView.textLable.detailLabel.

2.UICollectionCell自带了contentView,contentView.

1.UItableViewCell创建时直接使用,后来使用了重用机制+注册.

2.UICollectionViewCell只有注册.

共同点:二者都是继承于UIScrollView,够可以滚.

 1  UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];//初始化一个布局类CollectionView
 2     layout.itemSize = CGSizeMake(150, 200);   //设置每个Item大小
 3     layout.sectionInset = UIEdgeInsetsMake(20, 5, 10, 5); //设置每个item之间的间隙,上右下左
 4     layout.minimumLineSpacing = 15;  //最小行间距
 5     UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];//初始化一个CollectionView,并使用layout初始化
 6     
 7     collectionView.delegate = self;//设置代理和dataSource
 8     collectionView.dataSource = self;
 9     [self.view addSubview:collectionView];
10     [collectionView release];
11     [layout release];
12     
13     // 设置页眉的大小
14     layout.headerReferenceSize = CGSizeMake(320,40);
15     layout.footerReferenceSize = CGSizeMake(320, 20);
16     
17     // 注册 Cell
18     [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionCell];
19     
20     // 注册 页眉(header)
21     [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kElementHeadView];
22     // 页脚(footer)
23     [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kElementFootView];

代理UICollectionViewDataSource的协议中必须实现的两个方法:

// 返回每个分区里 item 的个数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {


}
//重用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

}
 1 // 点击 item 触发的方法
 2 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 3 
 4 }
 5 // 返回每个 item 的大小
 6 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
 7 
 8 }
 9 // 返回每个分区的缩进量
10 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
11 
12 }
13 
14 // 返回行间距
15 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
16    
17 }
18 // 返回 item 的间距
19 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
20 
21 }
22 
23 // 返回页眉的 size
24 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
25 
26 }
27 
28 // 返回页脚的 size
29 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
30 
31 }

  

 

原文地址:https://www.cnblogs.com/shaoting/p/4750700.html