上节讲的是基本的流水布局但是没讲自定义,此节简单的讲下自定义
//定义每个uicollectionview的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(80, 90);
}
上述方法是给collectionview添加大小,width和heith我们都自定义跟uitableview一样,uicollectionview自定义继承与uicollectionviewcell
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(0, 0, 100, 50);
self.button.backgroundColor = [UIColor orangeColor];
[self.button setTitle:@"测试" forState:0];
self.button.layer.cornerRadius = 5;
self.button.layer.masksToBounds = YES;
[self.contentView addSubview:self.button];
}
return self;
}
此处的frame很多不晓得怎么定义,其实就是对一个collectionview进行设置好比:cgsizemake(100,100);我们就对这100,100进行布局(不晓得我这样说算不算对的,不要误导大家了),假如要上半部分图片,下半部分btn,那么image的frame就是(0.0.100.70),btn的frame就是(0.0.100.30);
下面谈下一步
我们在uiviewcontroller里面初始化uicollectionview里面有个uicollectionviewcell
[collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
还有在我们必须实现的一个方法里也有个uicollectionviewcell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
赋值跟tableview的自定义一样,此处就不多说了,基本的的自定义就是这样了,假如要换成其他模式就是自定义层:uicollectionviewflowlayout,这个在接下来的几篇ccdn会写,以后我会附带上截图和录制动画,以后还望大神多多指点小蒲