Swift控件UICollectionView使用学习总结 - Swift
UICollectionView —— Swift
对 Swift开发UICollectionView的用法总结
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | import UIKit class ThirdViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { let item_identifier: String = "ThirdVCCollCell" let headerView_identifier: String = "ThirdVCCollHeaderView" let vcClassArr = [ "UIScrollViewStudyVc" , "UITextFieldStudyVc" , "UILabelStudyVc" ] var infoCollectionView:UICollectionView? = nil override func viewDidLoad() { super .viewDidLoad() print( "vcClassArr:\(vcClassArr)" ) view.addSubview( self .initInfoCollectionView()) // Do any additional setup after loading the view. } func initInfoCollectionView() -> UICollectionView { let layout = UICollectionViewFlowLayout() layout.itemSize = CGSize(width:80,height: 60) //设置item尺寸 layout.minimumLineSpacing = (UIScreen.main.bounds.width - 240) * 0.2 //上下间隔 layout.minimumInteritemSpacing = (UIScreen.main.bounds.width - 240) * 0.1 //左右间隔 layout.headerReferenceSize = CGSize(width:10,height: 30) //头部间隔 layout.footerReferenceSize = CGSize(width:0,height: 0) //底部间隔 layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10) //section四周的缩进 layout.scrollDirection = UICollectionViewScrollDirection.vertical //滚动方向 self .infoCollectionView = UICollectionView.init(frame: view.bounds, collectionViewLayout: layout) self .infoCollectionView?.delegate = self self .infoCollectionView?.dataSource = self self .infoCollectionView?.backgroundColor = UIColor.white //注册cell(使用xib自定义的collectionViewCell) self .infoCollectionView?. register (UINib.init(nibName: "ThirdVCCollCell" , bundle: nil ), forCellWithReuseIdentifier: item_identifier) //注册headerView self .infoCollectionView?. register (ThirdVCCollHeaderView. self , forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerView_identifier) return self .infoCollectionView! } func numberOfSections(in collectionView: UICollectionView) -> Int { return 6 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 11 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let itemCell: ThirdVCCollCell = collectionView.dequeueReusableCell(withReuseIdentifier: item_identifier, for : indexPath) as! ThirdVCCollCell itemCell.backgroundColor = UIColor.red; itemCell.setTitleLabelText(titleText: "第\(indexPath.section + 1)组" ) itemCell.setSubTitleLabelText(titleText: "第\(indexPath.item + 1)个Item" ) return itemCell; } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print( "当前点击第\(indexPath.section + 1)组,第\(indexPath.item + 1)个Item" ) if indexPath.section == 0{ if indexPath.row < 3{ let vcClassName = self .vcClassArr[indexPath.section] //字符串转 class let vcClass = NSClassFromString ( "swift_study1." + vcClassName) as! NSObject .Type; //"包名.类名" let vc:UIViewController = vcClass.init() as! UIViewController; vc.hidesBottomBarWhenPushed = true self .navigationController?.pushViewController(vc, animated: true ) } } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSize.init(width: self .view.frame.size.width, height: 30.0) } func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { if kind == UICollectionElementKindSectionHeader{ let headerView:ThirdVCCollHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerView_identifier, for : indexPath) as! ThirdVCCollHeaderView headerView.setTitleLabelText(title: "第\(indexPath.section)组" ) return headerView } else if kind == UICollectionElementKindSectionFooter{ } return UICollectionReusableView() } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
xib自定义collectionViewCell的方法:
其中自定义的collectionViewCell的.xib文件的布局约束和object-c的方式相同如下图
自定义collectionViewCell的.swift文件的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import UIKit class ThirdVCCollCell: UICollectionViewCell { @IBOutlet weak var title_label: UILabel! @IBOutlet weak var subTitle_label: UILabel! override func awakeFromNib() { super .awakeFromNib() // Initialization code } func setTitleLabelText(titleText:String){ if !titleText.isEmpty{ self .title_label.text = titleText } } func setSubTitleLabelText(titleText:String){ if !titleText.isEmpty{ self .subTitle_label.text = titleText } } } |
效果如下图:
感谢您的访问!
若对您有帮助或有兴趣请关注博客:http://www.cnblogs.com/Rong-Shengcom/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?