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

前端之家收集整理的这篇文章主要介绍了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")

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

原文链接:https://www.f2er.com/swift/320246.html

猜你在找的Swift相关文章