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

我正在尝试以编程方式创建UICollectionView.
我需要在单元格内添加标签,所以我创建了CollectionViewCell类.

这是班级:

import UIKit

class MyCollectionViewCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

这是collectionView实现类:

import UIKit

class 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).

我很乐意得到你的帮助.

你的问题就在这里.在viewDidLoad()中,您将两次注册collectionView单元格.您正在将collectionview的单元格注册到第一行中的自定义单元格类,然后在第二行中将其注册到类UICollectionViewCell.
collectionView.registerClass(MyCollectionViewCell.self,forCellWithReuseIdentifier: cellReuseIdentifier)
 collectionView.registerClass(UICollectionViewCell.self,forCellWithReuseIdentifier: "collectionCell")

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

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...