Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未显示Swift collectionViewCells_Swift_Uicollectionview - Fatal编程技术网

未显示Swift collectionViewCells

未显示Swift collectionViewCells,swift,uicollectionview,Swift,Uicollectionview,我有这个Swift密码 ` ` 但不幸的是,我的单元格没有显示。它只显示导航栏和背景色。我的代码可能有什么问题?试试看。 你在班上加了 UICollectionViewDataSource and UICollectionVIewDelegate. 请尝试以下代码: let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.sectionInset = UIEdgeInsets(to

我有这个Swift密码 `

` 但不幸的是,我的单元格没有显示。它只显示导航栏和背景色。我的代码可能有什么问题?

试试看。 你在班上加了

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
}