UICollectionView(网格布局)

初始化

@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);//每个区的空隙
        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);
 }];



@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"];

自定义 分区头部,尾部代理

#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  有内边距可修改为 bottom 才能正确移动到指定位置

##动态计算CollectionView 的行数
NSInteger row = titles.count % 3 ? titles.count / 3 + 1 : titles.count / 3;

#内容少也可滑动
_clvData.alwaysBounceVertical = YES;



遇到一些问题

UICollectionReusableView  用IB 注册的时候要拖线绑定才能正确显示出来
itemSize 的 width 取整
布局更新时候调用此方法,cell 插入 CollectionView 
self.collectionView.collectionViewLayout.invalidateLayout() 

---------获取clv 内容高度
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);
    }
}

https://blog.csdn.net/ayuapp/article/details/80360745

iOS 为CollectionView的分区添加背景色
https://blog.csdn.net/github_28024665/article/details/64125233

你可能感兴趣的:(UICollectionView(网格布局))