未显示Swift collectionViewCells
我有这个Swift密码 ` ` 但不幸的是,我的单元格没有显示。它只显示导航栏和背景色。我的代码可能有什么问题?试试看。 你在班上加了未显示Swift collectionViewCells,swift,uicollectionview,Swift,Uicollectionview,我有这个Swift密码 ` ` 但不幸的是,我的单元格没有显示。它只显示导航栏和背景色。我的代码可能有什么问题?试试看。 你在班上加了 UICollectionViewDataSource and UICollectionVIewDelegate. 请尝试以下代码: let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.sectionInset = UIEdgeInsets(to
UICollectionViewDataSource and UICollectionVIewDelegate.
请尝试以下代码:
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 60, height: 60)
let myCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
myCollectionView.dataSource = self
myCollectionView.delegate = self
myCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
myCollectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(myCollectionView)
在cellForItemAt数据源方法中,需要提供自定义UICollectionCell类
let cell=collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! FeedCell
cell.backgroundColor = .yellow
return cell
然后您将能够看到单元格。您必须提供集合视图单元格的大小。添加下面的代码段
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// calculate and return the height
}
你正在使用情节提要原型单元吗?@NiravD否我已删除情节提要你已注册单元吗?@NiravD
collectionView?.register(FeedCell.self,forCellWithReuseIdentifier:cellId)
将重载调用放在单元的注册之后这些类在UICollectionViewController
中是自动的,但请尝试。我将尝试在codeRedundant中查找您的问题“FeedController”与协议“UICollectionViewDataSource”的一致性尝试此dispatch_async(dispatch_get_main_queue()){self.collectionView.reloadData()}而不是self.collectionView.reloadData()你能发送屏幕快照吗请参考此url swift 3.0中的此url我的代码的类别是UICollectionViewController
不是UIViewController
尝试此url
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// calculate and return the height
}