iOS UICollectionView分组记录下

UICollectionView 的分组比TableView要复杂一点,在这里记录下把

总的说要这几步:‘

1、注册  UICollectionElementKindSectionHeader

2、新建 UICollectionReusableView 文件

3、设置代理方法

上代码:

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    
    [self.mainCollectionView registerClass:[MCInterestTagsCollectionViewCell class] forCellWithReuseIdentifier:@"MCInterestTagsCollectionViewCell"];
    [self.mainCollectionView registerClass:[MCInterestCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MCInterestCollectionReusableView"];
    
    [self.bgView addSubview:self.mainCollectionView];
    [self.mainCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(24);
        make.left.right.equalTo(self.bgView);
        make.bottom.equalTo(self.bgView.mas_bottom).offset(-kTabBarHeight);
    }];
#pragma mark - CollectionView数据源

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return self.alertSelectArr.count;
    }
    return self.couleSelectArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MCInterestTagsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MCInterestTagsCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.tageType = MCInterestTagsTypeAlert;
        cell.tagStr = [self.alertSelectArr objectOrNilAtIndex:indexPath.item];
        cell.deleteBtnClickBlock = ^{
            NSLog(@"删除:%@",[self.alertSelectArr objectOrNilAtIndex:indexPath.item]);
        };
    } else {
        cell.tageType = MCInterestTagsTypeCould;
        cell.tagStr = [self.couleSelectArr objectOrNilAtIndex:indexPath.item];
        cell.selectBtnClickBlock = ^{
            NSLog(@"选中:%@",[self.couleSelectArr objectOrNilAtIndex:indexPath.item]);
        };
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [MCInterestTagsCollectionViewCell cellSize];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 10, 0, 10);
}

// 每横行之间间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return CGFLOAT_MIN;
}

// 每竖行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return CGFLOAT_MIN;
}

//头部视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    MCInterestCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"MCInterestCollectionReusableView" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        headView.labTitle.text = @"已选兴趣标签";
    } else if (indexPath.section == 1) {
        headView.labTitle.text = @"可选兴趣标签";
    }
    return headView;
}

//头部视图frame
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(kScreenWidth, 66);
}

//底部视图fram
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeZero;
}

MCInterestCollectionReusableView.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

//collection 头部视图

@interface MCInterestCollectionReusableView : UICollectionReusableView

@property (nonatomic, strong) UILabel *labTitle;


@end

NS_ASSUME_NONNULL_END

MCInterestCollectionReusableView.m

#import "MCInterestCollectionReusableView.h"

@implementation MCInterestCollectionReusableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.labTitle = [[UILabel alloc] init];
        self.labTitle.font = [UIFont PingFangRegularFont:14];
        self.labTitle.textColor = [UIColor colorWithHexString:@"#292929"];
        [self addSubview:self.labTitle];
        [self.labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).offset(16);
        }];
    }
    return self;
}

@end
原文地址:https://www.cnblogs.com/qiyiyifan/p/11836433.html