UICollectionView
Posted pengyuan_D
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UICollectionView相关的知识,希望对你有一定的参考价值。
RootViewController.h
@interface RootViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
RootViewController.m
- (void)viewDidLoad
[super viewDidLoad];
//1.创建布局对象
UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
//设置滑动方向
flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
//设置item的尺寸
flowLayOut.itemSize = CGSizeMake(150, 150);
//设置相同行的item之间的最小间隙
flowLayOut.minimumInteritemSpacing = 10;
//设置每一行之间的距离
flowLayOut.minimumLineSpacing = 5;
//设置头视图的尺寸
flowLayOut.headerReferenceSize = CGSizeMake(100, 40);
//2.创建collectionView
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) collectionViewLayout:flowLayOut];
collectionView.backgroundColor = [UIColor greenColor];
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
//3.注册item
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell110"];
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"section_head"];
#pragma mark - UICollectionView dataSource
//设置组的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
return 2;
//设置每一组有多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
return 30;
//设置组的头视图,尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"section_head" forIndexPath:indexPath];
if (kind == UICollectionElementKindSectionHeader)
headerView.backgroundColor = [UIColor grayColor];
else if (kind == UICollectionElementKindSectionFooter)
headerView.backgroundColor = [UIColor redColor];
return headerView;
//创建item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
static NSString *iden = @"cell110";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:iden forIndexPath:indexPath];
NSString *name = [NSString stringWithFormat:@"%d@2x",indexPath.row+1];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
// cell.contentView 添加子视图
cell.backgroundView = imageView;
// cell.selectedBackgroundView
return cell;
//使用代理方法设置每一item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
int c = arc4random()%90;
return CGSizeMake(c+50, c+30);
//单元格的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
NSLog(@"%@",indexPath);
以上是关于UICollectionView的主要内容,如果未能解决你的问题,请参考以下文章
UICollectionView 总是自动调整单元格大小。不使用委托返回的尺寸
如何在第二个 Tailwind CSS 网格列中左对齐内容,使其在所有屏幕尺寸下都保持与第一列之间的间隙?