import UIKit //UICollectionViewLayout //itemSize属性 //设定全局的Cell尺寸,如果想要单独定义某个Cell的尺寸,可以使用下面方法: // - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath // //minimumLineSpacing属性 //设定全局的行间距,如果想要设定指定区内Cell的最小行距,可以使用下面方法: //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section // //minimumInteritemSpacing属性 //设定全局的Cell间距,如果想要设定指定区内Cell的最小间距,可以使用下面方法: //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section; // //scrollDirection属性 //设定滚动方向,有UICollectionViewScrollDirectionVertical和UICollectionViewScrollDirectionHorizontal两个值。 // //headerReferenceSize属性与footerReferenceSize属性 //设定页眉和页脚的全局尺寸,需要注意的是,根据滚动方向不同,header和footer的width和height中只有一个会起作用。如果要单独设置指定区内的页面和页脚尺寸,可以使用下面方法: //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section // //sectionInset属性 //设定全局的区内边距,如果想要设定指定区的内边距,可以使用下面方法: //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section; //因为UICollectionViewDelegateFlowLayout实际上是UICollectionViewDelegate的一个子协议,它继承了UICollectionViewDelegate,所以只需要在声明处写上UICollectionViewDelegateFlowLayout就行了。 class ViewController: UIViewController , UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout { let cellIdentity = "cellIdentity" let cellhead = "cellhead" let cellfoot = "cellfoot" let sectioncount = 1 let datas = ["cell1" , "cell2" , "cell3"] var collect:UICollectionView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.navigationItem.title = "image" self.view.backgroundColor = UIColor.whiteColor() let layout = UICollectionViewFlowLayout() collect = UICollectionView(frame: CGRectMake(10, 70, self.view.frame.size.width-20,self.view.frame.size.height-80) , collectionViewLayout: layout) collect.delegate = self collect.dataSource = self self.view.addSubview(collect) collect.backgroundColor = UIColor.whiteColor() collect.layer.borderWidth=1 collect.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: cellIdentity) collect.registerClass(CollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: cellhead) collect.registerClass(CollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: cellfoot) } //datasource func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return sectioncount } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return datas.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentity, forIndexPath: indexPath) cell.layer.borderWidth=1 for v in cell.contentView.subviews{ v.removeFromSuperview() } let lab = UILabel(frame: CGRectMake(10 , 10 , cell.frame.size.width-20 , cell.frame.size.height-20)) lab.numberOfLines = 0 lab.text = "\(datas[indexPath.item])(\(indexPath.section)-\(indexPath.item))" cell.contentView.addSubview(lab) return cell } //页眉页脚 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { return CGSizeMake(250, 50) } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSizeMake(250, 50) } func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) ->UICollectionReusableView { var reusable:CollectionReusableView! = nil if kind == UICollectionElementKindSectionHeader { reusable = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: cellhead, forIndexPath: indexPath) as! CollectionReusableView reusable.labText(cellhead) }else if kind == UICollectionElementKindSectionFooter { reusable = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: cellfoot, forIndexPath: indexPath) as! CollectionReusableView reusable.labText(cellfoot) } reusable.layer.borderWidth=1 return reusable } //UICollectionViewDelegateFlowLayout //设定collectionView(指定区)的边距 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5) } //设定指定区内Cell的最小间距,也可以直接设置UICollectionViewFlowLayout的minimumInteritemSpacing属性 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat{ return 10 } //设定指定区内Cell的最小行距,也可以直接设置UICollectionViewFlowLayout的minimumLineSpacing属性 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat { return 10 } //设定指定Cell的尺寸 func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSizeMake(200, 100) } //当指定indexPath处的item被选择时触发 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("didSelectItemAtIndexPath ++ \(indexPath)") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库