swift - 从 Storyboard (未嵌入)实例化的 Collection View 中未显示单元格

标签 swift uicollectionview uicollectionviewcell

在下面的场景中,我什至无法显示单元格背景(黑色)。

在 Storyboard 中,我有一个容器,其中嵌入了自定义 UICollectionView 和自定义 UICollectionViewCell,并以 ImageView 作为导出。在另一个 View Controller 中,我插入了一个容器,我想要相同的 Collection View 和单元格。

storyboard

我似乎能够实例化自定义 Collection View 和单元格(都打印到控制台并且不返回 nil)。以下代码打印如下。

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    print("into the collectionView")
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("photoCell" , forIndexPath: indexPath) as! PhotoCollectionViewCell

    cell.backgroundColor = UIColor.blackColor()
    let myRect = CGRect(x: 0, y: 0, width: 78, height: 78)
    cell.frame = myRect

    cell.autoresizesSubviews = false

    if let imageForCell = images?[indexPath.row]
    {
        cell.cellImageView.image = imageForCell
        print("cell image \(cell.cellImageView.image)")
    }
    print(cell.cellImageView?.bounds.size)
    cell.cellImageView?.layer.borderWidth = 1.0
    cell.cellImageView?.layer.borderColor = UIColor.blackColor().CGColor

    return cell
}

进入 Collection View

单元格图片可选(, {320, 213})

可选((78.0,78.0))

我已经尝试了所有建议的解决方案,但无法让单元格显示在 Collection View 中。

最佳答案

将 Controller 嵌入到两个容器中。所以无论你在 Controller 中做什么,它都会反射(reflect)在两个容器上

storyBoard

关于swift - 从 Storyboard (未嵌入)实例化的 Collection View 中未显示单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36154417/

相关文章:

ios - 模拟器上的 Xcode 错误 : MGIsDeviceOneOfType is not supported on this platform

ios - 如何自动调整单元格大小以适合每行 4 个单元格

ios - 在选择时为 UICollectionView 单元格设置动画

ios - SWIFT:通过单击 UIButton 将节/行插入 UICollectionView

ios - 如何更改 UICollectionViewCell 初始布局位置?

ios - UIButton 在我不希望它是透明的时候是透明的

ios - UICollectionView 保持水平列表滚动位置

ios - DidSelectRowAtIndexPath 在 Swift 2.0 中不起作用

ios - 如何保存照片和文字

ios - 无法使用属性访问.size : from String