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

相关推荐
2501_915909069 小时前
tcpdump 抓包数据分析实战,命令、过滤、常见故障定位与真机补充流程
网络·测试工具·ios·小程序·uni-app·iphone·tcpdump
Digitally12 小时前
如何将iPhone上的HEIF图像下载到电脑
ios·iphone
书弋江山12 小时前
iOS一直讲的单元格优化
macos·ios·cocoa
00后程序员张16 小时前
tcpdump 抓包分析,命令、过滤技巧、常见症状定位与移动真机补充方案
网络·测试工具·ios·小程序·uni-app·iphone·tcpdump
2501_9293826516 小时前
iphone IOS3~IOS9游戏 旧iphone 单机游戏合集分享
游戏·ios·iphone
2501_915921431 天前
iOS 26 电耗监测与优化,耗电问题实战 + 多工具 辅助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915921431 天前
苹果软件混淆与 iOS 应用加固白皮书,IPA 文件加密、反编译防护与无源码混淆方案全解析
android·ios·小程序·https·uni-app·iphone·webview
驰羽1 天前
[GO]Go语言泛型详解
开发语言·golang·xcode
猪哥帅过吴彦祖1 天前
Flutter 系列教程:列表与网格 - `ListView` 和 `GridView`
前端·flutter·ios
00后程序员张1 天前
Fiddler抓包工具使用教程,代理设置与调试方法实战解析(含配置技巧)
前端·测试工具·ios·小程序·fiddler·uni-app·webview