UICollectionView 01 - 基础布局篇
一,代码:
1.布局方式设置,创建UICollectionView
- (void)initailContentView { //导航 self.navigationBar = ({ CGFloat X = 0.0f; CGFloat Y = 0.0f; CGFloat W = [UIScreen mainScreen].bounds.size.width; CGFloat H = 44.f; UIView *view = [[UIView alloc]initWithFrame:CGRectMake(X, Y, W, H)]; view; }); [self.view addSubview:self.navigationBar]; //表视图 self.listView = ({ CGFloat X = 0.0f; CGFloat Y = CGRectGetMaxY(self.navigationBar.frame); CGFloat W = [UIScreen mainScreen].bounds.size.width; CGFloat H = ([UIScreen mainScreen].bounds.size.height - Y); //初始化layout UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init]; //设置布局方式为竖直布局 系统默认竖直布局 layout.scrollDirection = UICollectionViewScrollDirectionVertical; //设置同一列中间隔的cell最小间距 layout.minimumInteritemSpacing = 5.f; //设置最小行间距 layout.minimumLineSpacing = 5.f; //每个分区的上,左,下,右的缩进量 layout.sectionInset = UIEdgeInsetsMake(10.f, 10.f, 10.f, 10.f); //设置每个item的大小 CGFloat itemW = (W - layout.minimumLineSpacing - layout.sectionInset.left - layout.sectionInset.right)/2.f; CGFloat itemH = 80.f; layout.itemSize = CGSizeMake(itemW, itemH); //创建UICollectionView UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(X, Y, W, H) collectionViewLayout:layout]; collectionView.delegate = self; collectionView.dataSource = self; collectionView.showsVerticalScrollIndicator = NO; collectionView.showsHorizontalScrollIndicator = NO; collectionView.backgroundColor = [UIColor whiteColor]; collectionView; }); [self.view addSubview:self.listView]; //注册cell [self.listView registerClass:CustomCollectionViewCell.class forCellWithReuseIdentifier:NSStringFromClass(CustomCollectionViewCell.class)]; //注册header [self.listView registerClass:CustomCollectionReusableView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CustomCollectionReusableViewHeader"]; }
2.遵循代理 并实现数据源代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return self.dataSource.count; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataSource[section].items.count; } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(CustomCollectionViewCell.class) forIndexPath:indexPath]; cell.model = self.dataSource[indexPath.section].items[indexPath.item]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { } ///分区头 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { CustomCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CustomCollectionReusableViewHeader" forIndexPath:indexPath]; header.title = self.dataSource[indexPath.section].name; return header; } else { return [CustomCollectionReusableView new]; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeZero; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(CGRectGetWidth(collectionView.frame), 40.f); }
3.自定义UICollectionViewCell
#pragma mark - Initail Methods - (void)initailContentView { self.titleLbl = ({ CGFloat X = 0.0f; CGFloat W = CGRectGetWidth(self.frame); CGFloat H = 20.f; CGFloat Y = (CGRectGetHeight(self.frame) - H); UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(X, Y, W, H)]; lbl.textColor = [UIColor blackColor]; lbl.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular]; lbl; }); [self addSubview:self.titleLbl]; self.headerView = ({ CGFloat X = 0.0f; CGFloat Y = 0.0f; CGFloat W = CGRectGetWidth(self.frame); CGFloat H = CGRectGetMinY(self.titleLbl.frame); UIView *view = [[UIView alloc]initWithFrame:CGRectMake(X, Y, W, H)]; view.backgroundColor = [UIColor blueColor]; view; }); [self addSubview:self.headerView]; } - (void)setModel:(CustomItemModel *)model { _model = model; self.titleLbl.text = model.name; }
二,示例/demo
分类:
Ios
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
2019-06-27 【Flutter学习】页面跳转之路由及导航
2019-06-27 【Flutter学习】事件处理与通知之事件处理