美文网首页
2022-03-23

2022-03-23

作者: 上下求索zsh | 来源:发表于2022-03-23 00:03 被阅读0次

[转载] 通过 xib 创建自定义 UICollectionViewCell

云抱住阳光太阳没放弃发亮关注

0.072017.04.18 09:39:14字数 180阅读 4,639

原文链接: http://jingsan0801.farbox.com/post/swift/tong-guo-xibchuang-jian-zi-ding-yi-uicollectionviewcell

首先创建一个xib

之后会生成一个.swift和.xib文件,xib文件中是一个已经画好的View,我们要做的就是在这个View中布局cell中的控件,比如要增加一个Label和Button

并对这个xib中的view设置属性,包括identifier和custom class

然后在CustomCollectionViewCell中,将控件与变量建立关联,方法与在storyBoard中一样

CustomCollectionViewCell.swift

importUIKitclassCustomCollectionViewCell:UICollectionViewCell{@IBOutletweakvarlabel:UILabel!@IBOutletweakvarokBtn:UIButton!overridefuncawakeFromNib(){super.awakeFromNib()// 在这里实现对控件的布局}}

最后在ViewController.swift中加载这个cell,并对CollectionView的属性进行自定义

ViewController.swift

importUIKitclassViewController:UIViewController,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{// UICollectionViewDelegateFlowLayout已经实现了UICollectionViewDelegate: public protocol UICollectionViewDelegateFlowLayout : UICollectionViewDelegate@IBOutletweakvarcollectionView:UICollectionView!privateletcellIdentifier="collectionCell"overridefuncviewDidLoad(){super.viewDidLoad()self.collectionView.dataSource=selfself.collectionView.delegate=self// 从nib中注册cell,适用于通过xib创建cell的情况;如果是通过代码创建的cell,则使用registerClass方法collectionView.registerNib(UINib(nibName:"CustomCollectionViewCell",bundle:nil),forCellWithReuseIdentifier:cellIdentifier)}overridefuncdidReceiveMemoryWarning(){super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}// UICollectionViewDataSourcefuncnumberOfSectionsInCollectionView(collectionView:UICollectionView)->Int{return1}funccollectionView(collectionView:UICollectionView,numberOfItemsInSection section:Int)->Int{return10}funccollectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell{letcell=collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier,forIndexPath:indexPath)as!CustomCollectionViewCellcell.label.text="success"returncell}// UICollectionViewDelegateFlowLayoutfunccollectionView(collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath)->CGSize{returnCGSize(width:200,height:200)}funccollectionView(collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,minimumInteritemSpacingForSectionAtIndex section:Int)->CGFloat{return5.0}funccollectionView(collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,minimumLineSpacingForSectionAtIndex section:Int)->CGFloat{return1.0}funccollectionView(collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,insetForSectionAtIndex section:Int)->UIEdgeInsets{//        let frame : CGRect = self.view.frame//        let margin  = (frame.width - 90 * 3) / 6.0returnUIEdgeInsetsMake(1,1,10,1)// margin between cells}}

最终效果图

原文链接: http://jingsan0801.farbox.com/post/swift/tong-guo-xibchuang-jian-zi-ding-yi-uicollectionviewcell

相关文章

  • 橙子的ScalersTalk第六轮新概念朗读持续力训练Day 1

    练习材料:[Day 2736 2022-03-23] L32-2:A lost ship The captain ...

  • 2022-03-23

    2022-03-23坚持分享第1269天 读《建构解决之道》:P309-312感悟:联合系统资源与统整社区方案的弹...

  • 0142永澄老师送礼|成事技法教练公开课

    2022-03-23 北京 天晴今天是永澄老师的40岁生日,老师给我们送礼物来了!!!燃爆了~这节课价值千万,学...

  • 2022-03-23

    2022-03-23【15期同明内驱力亲子课践行90天@北京】打卡day80 【目标 】 我的第3个30天目标是:...

  • 孩子需要鼓励,就像植物需要水

    鼓励成长小故事分享#2022-03-23 故事一: 哥哥最近迷恋上了自制小飞机,他第一次做了一只纸飞机展示给我看,...

  • 耶殊陀尼诗社|看图作诗 独坐三月

    独坐三月 九宵罹难犹耳闻 独坐三月思归人 窗外绿篁窸窣响 疑是卿卿步履声 2022-03-23下午6:04

  • 饺面店的第三十六天:开业20天

    2022-03-23 开业整整20天,今天也小爆了一下,上午的生意不错,更喜欢现在的安排:早上5点起床,到店6点,...

  • 寻常

    寻常 2022-03-23 周三 晴 上午带心心念念到大行宫广场转转,人不多。学生都在上网课吧。跟心心念念同龄的孩...

  • 2022-03-24

    2022-03-23 人生·感悟 生活中每一个人都是独一无二的 无论平庸或优秀 请成为你想成为的自己 努力让自己活...

  • 健康:胃疼

    2022-03-23 从昨天晚上开始就不舒服,身上一阵阵的冷,不知道是感冒了还是胃疼病又范了,反正是很难受。今天上...

网友评论

      本文标题:2022-03-23

      本文链接:https://www.haomeiwen.com/subject/idoqjrtx.html