iOS38 UICollectionView的代码创建
2018-07-05 本文已影响11人
Abler
简单的代码创建, 方便个人copy
#import "ViewController.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat itemSpacing = 1.0; // cell 之间间距
int lineCellCount = 3; // 每行cell个数
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
layout.minimumInteritemSpacing = itemSpacing;
layout.itemSize =CGSizeMake((screenWidth - (lineCellCount - 1) * itemSpacing) / lineCellCount, 150);
UICollectionView *shareCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, screenWidth, 500) collectionViewLayout:layout];
shareCollectionView.backgroundColor = [UIColor blueColor];
[shareCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Flow"];
shareCollectionView.delegate = self;
shareCollectionView.dataSource = self;
[self.view addSubview:shareCollectionView];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
{
return 5;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Flow" forIndexPath:indexPath];
cell.backgroundColor = [UIColor grayColor];
// 分割线
CALayer *verticalSeparatorLine = [CALayer layer];
verticalSeparatorLine.backgroundColor = [UIColor redColor].CGColor;
verticalSeparatorLine.frame = CGRectMake(-1, 10, 1, 130);
[cell.contentView.layer addSublayer:verticalSeparatorLine];
return cell;
}
效果图