iOS collectionView XIB 非XIB 随笔
2019-11-11 本文已影响0人
奔跑吧小蚂蚁
XIB
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView registerNib:[UINib nibWithNibName:@"ZFHomeAdvertCell" bundle:nil] forCellWithReuseIdentifier:@"ZFHomeAdvertCell"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.minimumLineSpacing = 0;
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
flowLayout.itemSize = CGSizeMake(400, 60);
self.collectionView.collectionViewLayout = flowLayout;
}
#pragma mark--UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ZFHomeAdvertCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZFHomeAdvertCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
非XIB
#pragma mark - Lazy Loads
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
//水平方向间距
layout.minimumInteritemSpacing = 10;
//垂直方向间距
layout.minimumLineSpacing = 10;
//垂直方向
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
// 水平方向
// layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//设置内边距
layout.sectionInset = UIEdgeInsetsMake(20,20,20,20);
layout.itemSize = CGSizeMake((Screen_Width-40-20)/3, 80);
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.scrollEnabled = YES;
NSString *Identifier = [NSString stringWithFormat:@"CircleCategoryLabelCollectCell"];
[_collectionView registerNib:[UINib nibWithNibName:@"CircleCategoryLabelCollectCell" bundle:nil] forCellWithReuseIdentifier:Identifier];
}
return _collectionView;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.circleCategoryModel.data.count ? self.circleCategoryModel.data.count:0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *Identifier = [NSString stringWithFormat:@"CircleCategoryLabelCollectCell"];
CircleCategoryLabelCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Identifier forIndexPath:indexPath];
return cell;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}
设置每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
}