初始化
@property (nonatomic, strong) UICollectionView *collectionView;
- (UICollectionView *)collectionView
{
if (!_collectionView)
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);//每個區(qū)的空隙
layout.minimumInteritemSpacing = 10; //列與列之間的間距
layout.minimumLineSpacing = 10;//行與行之間的間距
layout.itemSize = CGSizeMake((SCREEN_WIDTH - 10)/2, 200);//cell的大小
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.delegate = self.manager;
_collectionView.dataSource = self.manager;
[_collectionView registerClass:[YZGMJCollectionCell class] forCellWithReuseIdentifier:@"YZGMJCollectionCell"];
}
return _collectionView;
}
[self.view addSubview:self.collectionView];
[_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArray;
- (NSMutableArray *)dataArray{
if(!_dataArray){
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
常用代理方法
#pragma mark ************** CollectionView 代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
YZGMJCollectionCell *cell = (YZGMJCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"YZGMJCollectionCell" forIndexPath:indexPath];
cell.cellClickBlack = ^(NSString *title){
self.cellClickBlack(title);
};
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%ld被點擊",indexPath.row);
}
注冊cell,SectionHead,SectionFoot
[_collectionView registerClass:[YZGMJCollectionCell class] forCellWithReuseIdentifier:@"YZGMJCollectionCell"];
[_collectionView registerClass:[YZGMJSectionHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YZGMJSectionHeadView"];
[_collectionView registerClass:[YZGMJSectionFootView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"YZGMJSectionFootView"];
UINib * sectionHeadNib = [UINib nibWithNibName:@"SmartBoxClvSectionHeadView" bundle:nil];
[collectionView registerNib: sectionHeadNib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SmartBoxClvSectionHeadView"];
自定義 分區(qū)頭部,尾部代理
#pragma mark ************** sectionHeadFootView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
ESWeakSelf;
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader )//頭部
{
YZGMJSectionHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YZGMJSectionHeadView" forIndexPath:indexPath];
reusableview = headerView;
}
if(kind == UICollectionElementKindSectionFooter)//尾部
{
YZGMJSectionFootView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"YZGMJSectionFootView" forIndexPath:indexPath];
reusableview = footerView;
}
return reusableview;
}
#pragma mark ************** sectionHeadView 尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(SCREEN_WIDTH,50);
}
#pragma mark ************** sectionFootView 尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeMake(SCREEN_WIDTH,50);
}
item Size 代理
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(SCREEN_WIDTH/2,100);
}
collectionView 方法特性
##水平滑動
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
##滾動到頂部
[_collectionViewList scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
UICollectionViewScrollPositionNone 有內(nèi)邊距可修改為 bottom 才能正確移動到指定位置
##動態(tài)計算CollectionView 的行數(shù)
NSInteger row = titles.count % 3 ? titles.count / 3 + 1 : titles.count / 3;
#內(nèi)容少也可滑動
_clvData.alwaysBounceVertical = YES;
#當(dāng)水平滾條到達(dá)終點所宰,總是(視圖)彈跳
_collectionView.alwaysBounceHorizontal = YES;
遇到一些問題
UICollectionReusableView 用IB 注冊的時候要拖線綁定才能正確顯示出來
itemSize 的 width 取整
布局更新時候調(diào)用此方法绒尊,cell 插入 CollectionView
self.collectionView.collectionViewLayout.invalidateLayout()
---------獲取clv 內(nèi)容高度
CGFloat height = self.clvData.collectionViewLayout.collectionViewContentSize.height;
---------tbvCell 嵌套 clv
CustomBasicInfoVC
iOS UICollectionView等分有1px縫隙
-(CGSize)collectionView:(UICollectionView* )collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger num = 4;
CGFloat width = CGRectGetWidth(collectionView.bounds)/num;
CGFloat height = 98;
if(indexPath.row == 0){
//第一列的width比其他列稍大一些,消除item之間的間隙
CGFloat realWidth = CGRectGetWidth(collectionView.bounds) - floor(width) * (num - 1);
return CGSizeMake(realWidth, height);
}else{
return CGSizeMake(floor(width), height);
}
}
UICollectionView解決item之間的間隙問題
https://blog.csdn.net/ayuapp/article/details/80360745
UICollectionView橫向滾動刷新控件
https://blog.csdn.net/weixin_42657552/article/details/81035005
iOS 為CollectionView的分區(qū)添加背景色
https://blog.csdn.net/github_28024665/article/details/64125233