English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
먼저 실제로 구현할 효과를 보도록 하겠습니다
다음은 예제 코드입니다
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]; // 뷰를 로드한 후 추가 설정을 수행합니다. 일반적으로 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. }
#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 == 2000) { self.btnHandelAction(@"left"); } if (tag == 2001) { self.btnHandelAction(@"right"); } } - (void)customBtnHandelAction:(BtnHandleAction)action{ self.btnHandelAction = action; } - (void)setup{ [self addSubview: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
정리
이 글의 모든 내용이 끝났습니다. 이 글의 내용이 여러분의 학습이나 업무에 도움이 되길 바랍니다. 의문이 있으시면 댓글을 통해 교류해 주세요.
선언: 이 글의 내용은 인터넷에서 가져왔으며, 저작권자가 소유하고 있으며, 인터넷 사용자가 자발적으로 기여하고 자체적으로 업로드한 내용입니다. 이 사이트는 소유권을 가지지 않으며, 인공적으로 편집한 것이 아니며, 관련 법적 책임을 부담하지 않습니다. 저작권 문제가 있으신 경우 notice#w로 이메일을 보내 주시기 바랍니다.3codebox.com에 (댓글을 작성할 때, #을 @으로 변경하여) 신고해 주시고 관련 증거를 제공하시면, 사이트가 즉시 의심스러운 저작권 내용을 삭제할 것입니다.