swift – 如何以编程方式创建UICollectionViewCell
发布时间:2020-12-14 05:49:25 所属栏目:百科 来源:网络整理
导读:我正在尝试以编程方式创建UICollectionView. 我需要在单元格内添加标签,所以我创建了CollectionViewCell类. 这是班级: import UIKitclass MyCollectionViewCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) } requir
我正在尝试以编程方式创建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 错误是: 我很乐意得到你的帮助.
你的问题就在这里.在viewDidLoad()中,您将两次注册collectionView单元格.您正在将collectionview的单元格注册到第一行中的自定义单元格类,然后在第二行中将其注册到类UICollectionViewCell.
collectionView.registerClass(MyCollectionViewCell.self,forCellWithReuseIdentifier: cellReuseIdentifier) collectionView.registerClass(UICollectionViewCell.self,forCellWithReuseIdentifier: "collectionCell") 只需删除第二行,您的代码就可以正常工作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |