First look at the effect diagram to be implemented
The sample code is as follows
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 no 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 isequaltostr
Ing:@ "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 = @ "Furniture";
Sectionview.rightstr = @ "Home appliance"; [Sectionview custombtnhandelaction:^ (NSString *leftorright)
{Self.clickindexpath = Indexpath;
Self.leftorright = Leftorright;
[CollectionView Reloaddata];
}];
return sectionview;
return nil;
}-(void) didreceivememorywarning {[Super didreceivememorywarning];
Dispose of any of the can is recreated. }
#import <UIKit/UIKit.h> typedef void (^btnhandleaction) (NSString *leftorright);
Interface Collectionsectionview:uicollectionreusableview @property (nonatomic,copy) NSString *leftstr;
@property (nonatomic,copy) NSString *rightstr;
-(void) Custombtnhandelaction: (btnhandleaction) Action; @end
#import "CollectionSectionView.h" @interface Collectionsectionview () @property (Nonatomic,strong) UIButton *leftbtn;
@property (Nonatomic,strong) UIButton *rightbtn;
@property (nonatomic,copy) btnhandleaction btnhandelaction; @end @implementation Collectionsectionview-(Instancetype) initWithFrame: (CGRect) frame {self = [super initWithFrame:
Frame];
if (self) {[self setup];
return self;
}-(UIButton *) leftbtn{if (!_leftbtn) {self.leftbtn = [UIButton buttonwithtype: (Uibuttontypecustom)];
_leftbtn.tag = 2000;
_leftbtn.frame = CGRectMake (1, 1, SELF.FRAME.SIZE.WIDTH/2-2, self.frame.size.height-2);
_leftbtn.backgroundcolor = [Uicolor Whitecolor];
[_leftbtn addtarget:self Action: @selector (btnaction:) forControlEvents: (UIControlEventTouchUpInside)];
return _leftbtn;
}-(UIButton *) rightbtn{if (!_rightbtn) {self.rightbtn = [UIButton buttonwithtype: (Uibuttontypecustom)];
_rightbtn.tag = 2001; _rightbtn.frame = CGRectMake (SELF.FRAME.SIZE.WIDTH/2 + 1, 1, SELF.FRAME.SIZE.WIDTH/2-2, self.frame.size.height-2);
_rightbtn.backgroundcolor = [Uicolor Whitecolor];
[_rightbtn addtarget:self Action: @selector (btnaction:) forControlEvents: (UIControlEventTouchUpInside)];
return _rightbtn;
}-(void) Btnaction: (UIButton *) sender{nsinteger tag = Sender.tag;
if (tag = =) {self.btnhandelaction (@ "left");
if (tag = = 2001) {Self.btnhandelaction (@ "right"); }-(void) Custombtnhandelaction: (btnhandleaction) action{self.btnhandelaction = Action;}-(void) setup{[self ADDSUBV
IEW:SELF.LEFTBTN];
[Self addSubview:self.rightBtn];
}-(void) Setleftstr: (NSString *) leftstr{[self.leftbtn settitle:leftstr forstate: (UIControlStateNormal)];
[Self.leftbtn Settitlecolor:[uicolor Blackcolor] forstate: (UIControlStateNormal)];
}-(void) Setrightstr: (NSString *) rightstr{[self.rightbtn settitle:rightstr forstate: (UIControlStateNormal)];
[Self.rightbtn Settitlecolor:[uicolor Blackcolor] forstate: (UIControlStateNormal)]; } @end
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can help, if there is doubt you can message exchange.