Ios CollectionView单元格未显示

Ios CollectionView单元格未显示,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我试图在UICollectionView中显示图像。下面的代码负责显示单元格 super.viewWillAppear(animated) memes = appDelegate.memes collectionView?.reloadData() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnVie

我试图在
UICollectionView
中显示图像。下面的代码负责显示单元格

    super.viewWillAppear(animated)
    memes = appDelegate.memes
    collectionView?.reloadData()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    let space:CGFloat = 3.0
    let dimension = (view.frame.size.width - (2 * space)) / 3.0

    flowLayout.minimumInteritemSpacing = space
    flowLayout.minimumLineSpacing = space
    flowLayout.itemSize = CGSize(width: dimension, height: dimension)
    flowLayout.sectionInset = UIEdgeInsets(top: 1.0, left: 1.0, bottom: 1.0, right: 1.0)
}


我看到的只是一个空白的视图控制器。当我点击屏幕时,详细信息会弹出(应该是这样的)。看起来视图的信息就在那里,但只是没有呈现在屏幕上。我需要做哪些更改才能确保图像显示?

不会调用您的collectionview代理。 你必须写作

collectionview.delegate=self
collectionview.dataSource=self

如果正在调用代理,请检查meme.meme是否有和image。

1)使用默认方法为collectionView提供flowLayout

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: width/3, height: 100)
    }

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{

        return  UIEdgeInsetsMake(0, 0, 0, 0)
    }

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{

        return 0
    }

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat{

        return 0
    }
2) 在重新加载collectionViw之前,请确保已在代码中进行了初始化

collectionview.delegate = self
 collectionview.dataSource = self

3) 重新编码后,在
numberOfItemsInSection
中添加断点,并检查meme数组是否真的具有重新加载CollectionView的值,如果是,当然会在这之后转到
CellforRowAt
并检查memeImageView是否获得值。使用断点打印图像视图只是为了试一试

结果是我没有在序列图像板的集合视图控制器中设置模块名称。

我遇到了同样的问题,当我调试该问题时,是集合视图无法显示高度,委托方法cellForItem甚至无法工作。 解决方案: 我将前导和尾随约束设置为stackView外部的父视图。 我的视图层次结构是parentView->stackView->collectionView。
如果您有任何问题,请发表评论

您的视图控制器是数据源吗?您的cellForItem是否曾经被调用过?如果您的memes.count>0 collectionView?.reloadData()@JoshHamet在这种情况下,数据源是应用程序委托中的一个数组,位于gets的cell for itemcalled@Anbu.Karthik我可以看到当有数据要显示时,它没有被显示。正在调用重新加载数据。。。未呈现任何内容您说您的
数据源
数组驻留在
AppDelegate
中。如何以及在何处将
AppDelegate
设置为
collectionView
dataSource
?简单地说,这条语句在哪里?
collectionView.dataSource=???
?只是尝试添加这两行代码…没有任何变化,因为您的单元格是nil add,如果cell==nil{collectionView.registerNib(UINib(nibName:“MemeCollectionViewCell”,bundle:nil),forCellReuseIdentifier:identifier)cell=collectionView.dequeueReusableCellWithIdentifier(identifier)为?MemeCollectionViewCell}我打印了单元格图像的描述,它是零。这很奇怪,因为我用来显示相同数据的表视图显示的是它应该显示的数据。我只是检查集合视图的自定义单元格中的imagview与代码之间的连接……ib插座似乎正在工作,只是finedata没有进入单元格这就是为什么它显示为黑屏,因为视图没有显示任何内容。您必须分两步检查Appdelegate中的meme数组,只需检查值是否真的变大是否在上诉中使用数组,如果是,则在您准备在任何地方使用上诉数组之前,打印其描述(如计数)。我只是检查单元格图像设置为的数据。它不是零。单元格本身不是nil,只有单元格.imageView?.image属性是nil。猜猜为什么会这样?你是想在ImageView中添加GIF而不是图像吗?
collectionview.delegate = self
 collectionview.dataSource = self