UICollectionViewController入门

  1. 自定义流布局继承自UICollectionViewFlowLayout

import

@interface YHPPictrueChoicesFlowLayout : UICollectionViewFlowLayout
@end

import "YHPPictrueChoicesFlowLayout.h"

@implementation YHPPictrueChoicesFlowLayout
/** 在 collectionView 第一次布局的时候调用,此时 collectionView 的 frame 已经设置完毕 */

  • (void)prepareLayout {
    // 一定 super
    [super prepareLayout];
    /** item大小 /
    self.itemSize = CGSizeMake(50, 50);
    /
    * 列间距 /
    self.minimumInteritemSpacing = 10;
    /
    * 行间距 /
    self.minimumLineSpacing = 10;
    /
    * 滚动方向 /
    self.scrollDirection = UICollectionViewScrollDirectionVertical;
    /
    * 弹簧效果 /
    self.collectionView.bounces = NO;
    /
    * 分页 /
    self.collectionView.pagingEnabled = YES;
    /
    * 水平滚动指示条 /
    self.collectionView.showsHorizontalScrollIndicator = NO;
    /
    * 垂直滚动指示条 */
    self.collectionView.showsVerticalScrollIndicator = NO;
    }
  1. 自定义cell

import

@interface YHPPictureChoiceCell : UICollectionViewCell
@end

import "YHPPictureChoiceCell.h"

@implementation YHPPictureChoiceCell
// collectionViewCell 的 frame 是根据之前的 layout 已经确定好的!

  • (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    /** 添加子控件 */
    }
    return self;
    }
    @end
    3.YHPPictureChoiceController

import

@interface YHPPictureChoiceController : UICollectionViewController
@end

import "YHPPictureChoiceController.h"

import "YHPPictrueChoicesFlowLayout.h"

import "YHPPictureChoiceCell.h"

@interface YHPPictureChoiceController ()
/** 自定义流布局 /
@property (nonatomic, strong) YHPPictrueChoicesFlowLayout layout;
@property (nonatomic, strong) YHPPictureChoiceCell cell;
@end
@implementation YHPPictureChoiceController
/
注册cell重用标识 /
static NSString * YHPPICTURECHOICECELL = @"YHPPictureChoiceCell";
/
懒加载cell /
-(YHPPictureChoiceCell )cell {
if (_cell == nil) {
_cell = [[YHPPictureChoiceCell alloc]init];
}
return _cell;
}
/
懒加载layout /
-(YHPPictrueChoicesFlowLayout )layout {
if(_layout == nil) {
_layout = [[YHPPictrueChoicesFlowLayout alloc]init];
}
return _layout;
}
/
重写init方法 */

  • (instancetype)init
    {
    self = [super initWithCollectionViewLayout:self.layout];
    if (self) {

    }
    return self;
    }
    /** viewDidLoad */

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    /** 注册cell */
    [self.collectionView registerClass:[YHPPictureChoiceCell class] forCellWithReuseIdentifier:YHPPICTURECHOICECELL];
    }

pragma mark - 数据源方法

/** 组数 */

  • (NSInteger)numberOfSectionsInCollectionView:(UICollectionView )collectionView {
    return 2;
    }
    /
    * 每组多少个Item */
  • (NSInteger)collectionView:(UICollectionView )collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
    }
    /
    * 返回某组某个cell */
  • (UICollectionViewCell *)collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath )indexPath {
    YHPPictureChoiceCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:YHPPICTURECHOICECELL forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    _cell = cell;
    return _cell;
    }
    /
    设置组间距 */
  • (UIEdgeInsets)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    /** 上 左 下 右 */
    return UIEdgeInsetsMake(10, 10, 0, 10);
    }

pragma mark 代理方法

@end


UICollectionViewController入门_第1张图片
collectionview.gif

你可能感兴趣的:(UICollectionViewController入门)