
那就从UICollectionView的使用开始吧 Let's go!
步骤:
1.布局方式设置,创建UICollectionView
//初始化layout
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
//设置同一列中间隔的cell最小间距
layout.minimumInteritemSpacing = 5;
//设置最小行间距
layout.minimumLineSpacing = 5;
//设置每个item的大小
layout.itemSize = CGSizeMake(collectionViewCellWidth, collectionViewCellHeight);
//设置每个区的 上、左、下、右的缩进量
layout.sectionInset=UIEdgeInsetsMake(10, 10, 20, 10);
//设置布局方式为竖直布局 系统默认竖直布局
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//创建UICollectionView
_collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:layout];
_collection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
_collection.delegate = self;
_collection.dataSource = self;
_collection.backgroundColor = [UIColor orangeColor];
_collection.showsHorizontalScrollIndicator = NO;
[self addSubview:_collection];
[_collection registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:registerIdentifier];
//masonry约束
/*[_collection mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.bottom.mas_equalTo(0);
}];*/
2.遵循代理 并实现数据源代理方法
static NSString *const registerIdentifier = @"collectionCell";
@interface CustomTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
数据源实现
#pragma mark- collectionview delegate & dataSource
//返回item个数
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CustomCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:registerIdentifier forIndexPath:indexPath];
cell.model = _dataArr[indexPath.row];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"点击%ld",indexPath.row);
}
3.自定义UICollectionViewCell
//CustomCollectionViewCell.h
@interface CustomCollectionViewCell : UICollectionViewCell
//传入数据model类
@property(nonatomic,strong)DataModel *model;
@end
@implementation CustomCollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.masksToBounds = YES;
[self createSubviews];
}
return self;
}
-(void)createSubviews{
self.backgroundColor = [UIColor whiteColor];
//自定义视图添加。。。
}
以上就是基本的创建使用了,用你的鼠标右键尽情挥洒吧。
此篇比较基础大佬请忽略,如有不当地方请指正,小弟虚心接受!时间有限今天就先写这么多,此后会陆续更新,逐步深入,
关键是下班了,哈哈哈哈!go go go go go home!!! 这才是最开心的事。。。。
网友评论