- 自定義流布局繼承自UICollectionViewFlowLayout
import <UIKit/UIKit.h>
@interface YHPPictrueChoicesFlowLayout : UICollectionViewFlowLayout
@end
import "YHPPictrueChoicesFlowLayout.h"
@implementation YHPPictrueChoicesFlowLayout
/** 在 collectionView 第一次布局的時(shí)候調(diào)用铭若,此時(shí) collectionView 的 frame 已經(jīng)設(shè)置完畢 */
- (void)prepareLayout {
// 一定 super
[super prepareLayout];
/** item大小 /
self.itemSize = CGSizeMake(50, 50);
/* 列間距 /
self.minimumInteritemSpacing = 10;
/* 行間距 /
self.minimumLineSpacing = 10;
/* 滾動(dòng)方向 /
self.scrollDirection = UICollectionViewScrollDirectionVertical;
/* 彈簧效果 /
self.collectionView.bounces = NO;
/* 分頁 /
self.collectionView.pagingEnabled = YES;
/* 水平滾動(dòng)指示條 /
self.collectionView.showsHorizontalScrollIndicator = NO;
/* 垂直滾動(dòng)指示條 */
self.collectionView.showsVerticalScrollIndicator = NO;
}
- 自定義cell
import <UIKit/UIKit.h>
@interface YHPPictureChoiceCell : UICollectionViewCell
@end
import "YHPPictureChoiceCell.h"
@implementation YHPPictureChoiceCell
// collectionViewCell 的 frame 是根據(jù)之前的 layout 已經(jīng)確定好的暗膜!
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
/** 添加子控件 */
}
return self;
}
@end
3.YHPPictureChoiceController
import <UIKit/UIKit.h>
@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
/ 注冊(cè)cell重用標(biāo)識(shí) /
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];
/** 注冊(cè)cell */
[self.collectionView registerClass:[YHPPictureChoiceCell class] forCellWithReuseIdentifier:YHPPICTURECHOICECELL];
}
pragma mark - 數(shù)據(jù)源方法
/** 組數(shù) */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView )collectionView {
return 2;
}
/* 每組多少個(gè)Item */ - (NSInteger)collectionView:(UICollectionView )collectionView numberOfItemsInSection:(NSInteger)section {
return 10;
}
/* 返回某組某個(gè)cell */ - (UICollectionViewCell *)collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath )indexPath {
YHPPictureChoiceCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:YHPPICTURECHOICECELL forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
_cell = cell;
return _cell;
}
/ 設(shè)置組間距 */ - (UIEdgeInsets)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
/** 上 左 下 右 */
return UIEdgeInsetsMake(10, 10, 0, 10);
}
pragma mark 代理方法
@end