UITableView 和 UICollectionView的设计思路:
1.UITableView 的布局由TableView和UItableViewDelegate完成.
2.UICollectionView布局样式由UICollectionViewFlowLayout和UICollectionViewDelegate完成.
数据源:
1.UITableView由UITableViewDataSource提供.
2.UICollectionView由UICollectionViewDataSource提供.
布局样式:
1.UITableView是多列单行.
2.UICollectionView是多行多列.
1.UITableViewCell上自带imageView.textLable.detailLabel.
2.UICollectionCell自带了contentView,contentView.
1.UItableViewCell创建时直接使用,后来使用了重用机制+注册.
2.UICollectionViewCell只有注册.
共同点:二者都是继承于UIScrollView,够可以滚.
1 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];//初始化一个布局类CollectionView 2 layout.itemSize = CGSizeMake(150, 200); //设置每个Item大小 3 layout.sectionInset = UIEdgeInsetsMake(20, 5, 10, 5); //设置每个item之间的间隙,上右下左 4 layout.minimumLineSpacing = 15; //最小行间距 5 UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];//初始化一个CollectionView,并使用layout初始化 6 7 collectionView.delegate = self;//设置代理和dataSource 8 collectionView.dataSource = self; 9 [self.view addSubview:collectionView]; 10 [collectionView release]; 11 [layout release]; 12 13 // 设置页眉的大小 14 layout.headerReferenceSize = CGSizeMake(320,40); 15 layout.footerReferenceSize = CGSizeMake(320, 20); 16 17 // 注册 Cell 18 [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionCell]; 19 20 // 注册 页眉(header) 21 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kElementHeadView]; 22 // 页脚(footer) 23 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kElementFootView];
代理UICollectionViewDataSource的协议中必须实现的两个方法:
1 2 3 4 5 6 7 8 | // 返回每个分区里 item 的个数 - ( NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:( NSInteger )section { } //重用 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath{<br><br>} |
1 // 点击 item 触发的方法 2 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 3 4 } 5 // 返回每个 item 的大小 6 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 7 8 } 9 // 返回每个分区的缩进量 10 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 11 12 } 13 14 // 返回行间距 15 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { 16 17 } 18 // 返回 item 的间距 19 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 20 21 } 22 23 // 返回页眉的 size 24 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { 25 26 } 27 28 // 返回页脚的 size 29 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { 30 31 }
分类:
iOS学习-UI
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探