collection的简单使用


添加代理

UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

添加UICollectionViewFlowLayout

    CGFloat viewWidth = [UIScreen mainScreen].bounds.size.width;

    CGFloat viewHeight = [UIScreen mainScreen].bounds.size.height;

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

    // 设置每个item的大小,

    layout.itemSize = CGSizeMake(90, 90);

    // 设置列的最小间距

    layout.minimumInteritemSpacing = 10;

    // 设置最小行间距

    layout.minimumLineSpacing = 20;

    // 设置布局的内边距

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

    // 滚动方向

    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

添加UICollectionView

    UICollectionView *aCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 100, viewWidth-20, viewHeight-300) collectionViewLayout:layout];

    aCollectionView.backgroundColor = [UIColor whiteColor];

    aCollectionView.delegate = self;

    aCollectionView.dataSource = self;

    [aCollectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    [self.view addSubview:aCollectionView];

实现代理方法

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

    return 80;

}

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

    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    cell.contentView.backgroundColor = [UIColor grayColor];

    cell.topImg.image = [UIImage imageNamed:@"icn_choice"];

    cell.titleLbl.text = @"hello";

    return cell;

}

UICollectionViewDelegateFlowLayout代理方法

//动态设置每个Item的尺寸大小

- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath;

//动态设置每个分区的EdgeInsets

- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;

//动态设置每行的间距大小

- (CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;

//动态设置每列的间距大小

- (CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

//动态设置某组头视图大小

- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;

//动态设置某组尾视图大小

- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

自定义UICollectionViewCell

CustomCollectionViewCell.H文件

#import@interface CustomCollectionViewCell : UICollectionViewCell

@property (nonatomic,strong)UILabel* titleLbl;

@property (nonatomic,strong)UIImageView* topImg;

@end

CustomCollectionViewCell.M文件


#import "CustomCollectionViewCell.h"

@implementation CustomCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {


    }

    return self;

}

- (UILabel*)titleLbl{

    if (_titleLbl == nil) {

        _titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.frame.size.width-20, 20)];

        _titleLbl.textColor = [UIColor greenColor];

        [self.contentView addSubview:_titleLbl];

    }

    return _titleLbl;

}

- (UIImageView*)topImg{

    if (_topImg == nil) {

        _topImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

        [self.contentView addSubview:_topImg];

    }

    return _topImg;

}

@end

你可能感兴趣的:(collection的简单使用)