swift – 如何以编程方式创建UICollectionViewCell

swift – 如何以编程方式创建UICollectionViewCell,第1张

概述我正在尝试以编程方式创建UICollectionView. 我需要在单元格内添加标签,所以我创建了CollectionViewCell类. 这是班级: import UIKitclass MyCollectionViewCell: UICollectionViewCell { override init(frame: CGRect) { super.init(fram 我正在尝试以编程方式创建UICollectionVIEw.
我需要在单元格内添加标签,所以我创建了CollectionVIEwCell类.

这是班级:

import UIKitclass MyCollectionVIEwCell: UICollectionVIEwCell {    overrIDe init(frame: CGRect) {        super.init(frame: frame)    }    required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }}

这是collectionVIEw实现类:

import UIKitclass TwoVIEwController: UIVIEwController,UICollectionVIEwDataSource,UICollectionVIEwDelegateFlowLayout,UICollectionVIEwDelegate {    let leftAndRightpaddings: CGfloat = 80.0    let numberOfItemsPerRow: CGfloat = 7.0    let screenSize: CGRect = UIScreen.mainScreen().bounds    private let cellReuseIDentifIEr = "collectionCell"    var items = ["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"]    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        let flowLayout = UICollectionVIEwFlowLayout()        let collectionVIEw = UICollectionVIEw(frame: self.vIEw.bounds,collectionVIEwLayout: flowLayout)        collectionVIEw.registerClass(MyCollectionVIEwCell.self,forCellWithReuseIDentifIEr: cellReuseIDentifIEr)        collectionVIEw.registerClass(UICollectionVIEwCell.self,forCellWithReuseIDentifIEr: "collectionCell")        collectionVIEw.delegate = self        collectionVIEw.dataSource = self        collectionVIEw.backgroundcolor = UIcolor.cyancolor()        self.vIEw.addSubvIEw(collectionVIEw)    }    func collectionVIEw(collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int    {        return self.items.count    }    func collectionVIEw(collectionVIEw: UICollectionVIEw,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionVIEwCell    {        let cell = collectionVIEw.dequeueReusableCellWithReuseIDentifIEr(cellReuseIDentifIEr,forIndexPath: indexPath) as! MyCollectionVIEwCell        cell.backgroundcolor = UIcolor.greencolor()        return cell    }    func collectionVIEw(collectionVIEw: UICollectionVIEw,layout collectionVIEwLayout: UICollectionVIEwLayout,sizeforItemAtIndexPath indexPath: NSIndexPath) -> CGSize    {        let wIDth = (screenSize.wIDth-leftAndRightpaddings)/numberOfItemsPerRow        return CGSizeMake(wIDth,wIDth)    }    func collectionVIEw(collectionVIEw: UICollectionVIEw,insetForSectionAtIndex section: Int) -> UIEdgeInsets    {        return UIEdgeInsets(top: 20,left: 8,bottom: 5,right: 8)    }}

生成单元格时发生错误:

func collectionVIEw(collectionVIEw: UICollectionVIEw,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionVIEwCell{    let cell = collectionVIEw.dequeueReusableCellWithReuseIDentifIEr(cellReuseIDentifIEr,forIndexPath: indexPath) as! MyCollectionVIEwCell

错误是:
无法将’UICollectionVIEwCell'(0x1033cc820)类型的值转换为’CollectionVIEwProgramatically.MyCollectionVIEwCell'(0x1015a4f88).

我很乐意得到你的帮助.

@H_301_22@ 你的问题就在这里.在vIEwDIDLoad()中,您将两次注册collectionVIEw单元格.您正在将collectionvIEw的单元格注册到第一行中的自定义单元格类,然后在第二行中将其注册到类UICollectionVIEwCell.
collectionVIEw.registerClass(MyCollectionVIEwCell.self,forCellWithReuseIDentifIEr: cellReuseIDentifIEr) collectionVIEw.registerClass(UICollectionVIEwCell.self,forCellWithReuseIDentifIEr: "collectionCell")

只需删除第二行,您的代码就可以正常工作.

总结

以上是内存溢出为你收集整理的swift – 如何以编程方式创建UICollectionViewCell全部内容,希望文章能够帮你解决swift – 如何以编程方式创建UICollectionViewCell所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1051884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存