首先来看看要实现的效果图

示例代码如下
Untitled.gif
#import "ViewController.h"
#import "CollectionViewCell.h"
#import "CollectionSectionView.h"
@interface ViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,copy)NSString *leftOrRight;
@property (nonatomic,strong)NSIndexPath *clickIndexPath;
@end
static NSString *cellIdentifier = @"CollectionViewCell";
static NSString *sectionIdentifier = @"CollectionSectionView";
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(50, 25);
flowLayout.minimumLineSpacing = 0;
flowLayout.minimumInteritemSpacing = 0;
flowLayout.headerReferenceSize = CGSizeMake(0, 40);
flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
[_collectionView registerClass:[CollectionSectionView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:sectionIdentifier];
[self.view addSubview:_collectionView];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (self.clickIndexPath.section == section) {
if (self.clickIndexPath.section == 0 && [self.leftOrRight isEqualToString:@"left"]) {
return 3;
}
if (self.clickIndexPath.section == 0 && [self.leftOrRight isEqualToString:@"right"]) {
return 4;
}
if (self.clickIndexPath.section == 1 && [self.leftOrRight isEqualToString:@"left"]) {
return 10;
}
if (self.clickIndexPath.section == 1 && [self.leftOrRight isEqualToString:@"right"]) {
return 8;
}
if (self.clickIndexPath.section == 2 && [self.leftOrRight isEqualToString:@"left"]) {
return 33;
}
if (self.clickIndexPath.section == 2 && [self.leftOrRight isEqualToString:@"right"]) {
return 6;
}
}
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.label.text = [NSString stringWithFormat:@"%ld-%ld",indexPath.section,indexPath.row];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
CollectionSectionView *sectionView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:sectionIdentifier forIndexPath:indexPath];
sectionView.backgroundColor = [UIColor lightGrayColor];
sectionView.leftStr = @"家具";
sectionView.rightStr = @"家电";
[sectionView customBtnHandelAction:^(NSString *leftOrRight) {
self.clickIndexPath = indexPath;
self.leftOrRight = leftOrRight;
[collectionView reloadData];
}];
return sectionView;
}
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}










