CollectionView Cells未显示

时间:2017-08-01 06:09:37

标签: ios swift uicollectionview

我试图在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)
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MemeCollectionViewCell", for: indexPath) as! MemeCollectionViewCell
    let meme = memes[(indexPath as NSIndexPath).row]

    cell.memeImageView?.image = meme.meme
    // Configure the cell

    return cell
}

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

4 个答案:

答案 0 :(得分:1)

事实证明我没有在故事板中的集合视图控制器中设置模块名称。

答案 1 :(得分:0)

您的collectionview代表未被调用。 你必须写

collectionview.delegate = self collectionview.dataSource = self

如果代表被调用,请检查meme.meme是否有图像。

答案 2 :(得分:0)

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

console.log(value.reverse());

2)确保在重新加载collectionViw之前已在代码中初始化

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
    }

3)在重新编码之后在collectionview.delegate = self collectionview.dataSource = self 中添加断点并检查你的meme数组是否真的有值你重新加载CollectionView,如果是,当然它会在此之后转到numberOfItemsInSection并检查你的memeImageView是否有价值。使用断点打印图像视图只是为了尝试

答案 3 :(得分:0)

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

相关问题