美文网首页
iOS38 UICollectionView的代码创建

iOS38 UICollectionView的代码创建

作者: Abler | 来源:发表于2018-07-05 11:15 被阅读11次

简单的代码创建, 方便个人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;
}
效果图

相关文章

网友评论

      本文标题:iOS38 UICollectionView的代码创建

      本文链接:https://www.haomeiwen.com/subject/yewuuftx.html