ios - XCode7 : "Failed to render instance of" affects all UICollectionViewCells

标签 ios xcode swift storyboard uicollectionviewcell

我目前在 Storyboard 中使用 UICollectionViewCells 时总是出错。没有其他控件显示此行为。我怎样才能摆脱它们?

Storyboard errors

这是受影响的 CollectionViewCells 之一的样子:

enter image description here

我是这样定义的:

UICollectionViewCell Definition

这是 CategoryCollectionCell

的代码
import UIKit
import Foundation

@IBDesignable class CategoryCollectionCell : UICollectionViewCell {

    @IBOutlet private weak var imageView: UIImageView!
    @IBOutlet private weak var label: UILabel!

    internal var id : Int?

    override var highlighted : Bool {
        didSet {
            label.textColor = highlighted ? UIColor.greenColor() : UIColor.whiteColor()
        }
    }

    @IBInspectable var text : String? {
        get { return label.text }
        set(value) { label.text = value }
    }

    @IBInspectable var image : UIImage? {
        get { return imageView.image }
        set(value) { imageView.image = value }
    }
}

这是 CollectionViewController 的代码:

extension CategoryViewController : UICollectionViewController {

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = self.collectionView?.dequeueReusableCellWithReuseIdentifier(kReuseCellIdentifier, forIndexPath: indexPath)
        var categoryCollectionCell = cell as? CategoryCollectionCell
        if categoryCollectionCell == nil {
            categoryCollectionCell = CategoryCollectionCell()
        }

        let data = getDataForIndexPath(indexPath)
        if data != nil {
            categoryCollectionCell?.id = data!.id
            categoryCollectionCell!.text = data!.category
            categoryCollectionCell!.image = data!.image
        }

        return categoryCollectionCell!
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
}

extension CategoryViewController : UICollectionViewDelegateFlowLayout {
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        guard let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout else {
            return CGSize()
        }

        let width = CGRectGetWidth(collectionView.bounds)
        let padding = flowLayout.sectionInset.left + flowLayout.sectionInset.right
        let itemSpacing = flowLayout.minimumInteritemSpacing
        let size = (width - padding - itemSpacing) / 2
        return CGSize(width: size, height: size)
    }
}

最佳答案

好的。我发现 XCode 显示的错误与实际问题无关。

目录 /Users/{Username}/Library/Logs/DiagnosticReports 应该包含名称如下的文件:IBDesignablesAgentCocoaTouch[...].crash

在它们里面,我发现了堆栈跟踪,这让我找到了真正的问题: 问题出在自定义 UITableViewCell 而不是 UICollectionViewCell

的代码中
class FooTableCell : UITableViewCell {

    @IBOutlet private weak var checkmarkImageView: UIImageView!

    override internal var selected : Bool {
        didSet {
            checkmarkImageView.hidden = !selected
        }
    }
}

使用设计器时,checkmarkImageViewnil。因此,Cocoa Storyboard Agent 崩溃了。

我通过添加保护语句修复了它:

class FooTableCell : UITableViewCell {

    @IBOutlet private weak var checkmarkImageView: UIImageView!

    override internal var selected : Bool {
        didSet {
            guard let imageView = checkmarkImageView else {
                return
            }

            imageView.hidden = !selected
        }
    }
}

关于ios - XCode7 : "Failed to render instance of" affects all UICollectionViewCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33752866/

相关文章:

xcode - ITMS-90745:无效的工具链

iOS UITapGestureRecognizer 有时不工作

objective-c - 播放声音与显示的图像一一对应

ios - 在创建Observable/Single时将传递给ScheduleOn的调度程序

ios - 将 UIColor 值分配给宏

ios - 在 Xcode ui 测试 Swift 中查找表格单元格

ios - 如何提供TestFlight iOS beta应用程序的反馈?

swift - 无法在 macOS 上显示简单通知

swift - "public"在 Swift 中意味着什么?

swift - iOS : Notification Badge Number does not reset