iOS UIColletionView实现header和footter

// ProfileViewController.m

// scxhgh

// Created by xmkjsoft on 2024/7/16.

#import "ProfileViewController.h"

@interface ProfileViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView; // 声明UICollectionView属性

@end

@implementation ProfileViewController

  • (void)viewDidLoad {

super viewDidLoad\]; // 设置背景色 self.view.backgroundColor = \[UIColor whiteColor\]; // 创建并设置UICollectionView \[self setupCollectionView\]; } - (void)setupCollectionView { UICollectionViewFlowLayout \*layout = \[\[UICollectionViewFlowLayout alloc\] init\]; layout.itemSize = CGSizeMake(100, 100); // 设置每个cell的大小 layout.minimumLineSpacing = 10; // 设置行间距 layout.minimumInteritemSpacing = 10; // 设置列间距 layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); // 设置section的内边距 // 设置header的大小 layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 50); // 设置footer的大小 layout.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 50); self.collectionView = \[\[UICollectionView alloc\] initWithFrame:self.view.bounds collectionViewLayout:layout\]; self.collectionView.dataSource = self; self.collectionView.delegate = self; self.collectionView.backgroundColor = \[UIColor whiteColor\]; // 注册cell类 \[self.collectionView registerClass:\[UICollectionViewCell class\] forCellWithReuseIdentifier:@"cellIdentifier"\]; // 注册header类 \[self.collectionView registerClass:\[UICollectionReusableView class\] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerIdentifier"\]; // 注册footer类 \[self.collectionView registerClass:\[UICollectionReusableView class\] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerIdentifier"\]; \[self.view addSubview:self.collectionView\]; } - (UICollectionReusableView \*)collectionView:(UICollectionView \*)collectionView viewForSupplementaryElementOfKind:(NSString \*)kind atIndexPath:(NSIndexPath \*)indexPath { if (\[kind isEqualToString:UICollectionElementKindSectionHeader\]) { UICollectionReusableView \*headerView = \[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerIdentifier" forIndexPath:indexPath\]; // 移除之前的所有子视图,避免复用问题 \[headerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)\]; // 添加一个UILabel到headerView UILabel \*titleLabel = \[\[UILabel alloc\] initWithFrame:headerView.bounds\]; titleLabel.text = @"这是Header"; titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.textColor = \[UIColor blackColor\]; \[headerView addSubview:titleLabel\]; return headerView; } else if (\[kind isEqualToString:UICollectionElementKindSectionFooter\]) { UICollectionReusableView \*footerView = \[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerIdentifier" forIndexPath:indexPath\]; // 移除之前的所有子视图,避免复用问题 \[footerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)\]; // 添加一个UILabel到footerView UILabel \*footerLabel = \[\[UILabel alloc\] initWithFrame:footerView.bounds\]; footerLabel.text = @"这是Footer"; footerLabel.textAlignment = NSTextAlignmentCenter; footerLabel.textColor = \[UIColor grayColor\]; \[footerView addSubview:footerLabel\]; return footerView; } return nil; } #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView \*)collectionView { return 1; // 返回section的数量 } - (NSInteger)collectionView:(UICollectionView \*)collectionView numberOfItemsInSection:(NSInteger)section { return 5; // 返回每个section中item的数量 } - (UICollectionViewCell \*)collectionView:(UICollectionView \*)collectionView cellForItemAtIndexPath:(NSIndexPath \*)indexPath { UICollectionViewCell \*cell = \[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath\]; cell.backgroundColor = \[UIColor systemBlueColor\]; // 设置cell的背景色 // 可以在这里添加更多的自定义内容,比如在cell上添加一个UILabel UILabel \*label = \[\[UILabel alloc\] initWithFrame:cell.contentView.bounds\]; label.text = \[NSString stringWithFormat:@"%ld", (long)indexPath.row\]; label.textAlignment = NSTextAlignmentCenter; label.textColor = \[UIColor whiteColor\]; \[cell.contentView addSubview:label\]; return cell; } #pragma mark - UICollectionViewDelegateFlowLayout - (CGSize)collectionView:(UICollectionView \*)collectionView layout:(UICollectionViewLayout\*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath \*)indexPath { // 设置列数 NSInteger numberOfColumns = 5; // 获取当前屏幕的宽度 CGFloat totalWidth = collectionView.bounds.size.width; // 设置section的内边距(与layout.sectionInset一致) CGFloat sectionInset = 10.0; // 设置item之间的最小列间距(与layout.minimumInteritemSpacing一致) CGFloat minimumInteritemSpacing = 10.0; // 计算item的宽度 CGFloat itemWidth = (totalWidth - (sectionInset \* 2) - ((numberOfColumns - 1) \* minimumInteritemSpacing)) / numberOfColumns; // 返回item的大小 return CGSizeMake(itemWidth, itemWidth); } @end

相关推荐
MaoJiu1 天前
Flutter混合开发:在iOS工程中嵌入Flutter Module
flutter·ios
2501_915921431 天前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_916008891 天前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview
Digitally1 天前
如何将视频从 iPhone 转移到 Mac
macos·ios·iphone
2501_916007471 天前
uni-app iOS 文件调试常见问题与解决方案:结合 itools、克魔、iMazing 的实战经验
android·ios·小程序·https·uni-app·iphone·webview
豆豆(设计前端)1 天前
使用 Uni-app 打包 外链地址APK 及 iOS 注意事项
ios·uni-app
关键帧-Keyframe2 天前
音视频面试题集锦第 38 期
macos·面试·音视频·cocoa
库奇噜啦呼2 天前
属性关键字
ios
2501_915918412 天前
uni-app 项目 iOS 上架踩坑经验总结 从证书到审核的避坑指南
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者82 天前
iOS 上架 uni-app 流程全解析,从打包到发布的完整实践
android·ios·小程序·https·uni-app·iphone·webview