UICollectionView

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

    CGFloat itemWidth = (kScreenWidth - 30) / 2;

    CGFloat itemHeight = 260;

    layout.itemSize = CGSizeMake(itemWidth, itemHeight);

    layout.minimumInteritemSpacing = 10;// 左右的最小距离

    layout.minimumLineSpacing = 10;// 上下的最小距离

    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;// 水平滑动

    layout.footerReferenceSize = CGSizeMake(kScreenWidth, 150);

    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

    

    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:layout];

    collection.backgroundColor = RGB(240, 241, 242);

    [collection registerClass:[UICollectionView class] forCellWithReuseIdentifier:@"cell"];

    collection.delegate = self;

    collection.dataSource = self;

    [self.view addSubview:collection];

 

 

#pragma UICollectionView dataSource

 

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

    

    return 5;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    

    static NSString *identify = @"collectionCell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];

    if (!cell) {

        cell = [[UICollectionView alloc] init];

    }

    cell.backgroundColor = [UIColor whiteColor];

    return cell;

}

原文地址:https://www.cnblogs.com/tom2015010203/p/5230687.html