简体   繁体   English

XCode7:“无法呈现其实例”会影响所有UICollectionViewCells

[英]XCode7: “Failed to render instance of” affects all UICollectionViewCells

I currently always get an error when using UICollectionViewCells in a Storyboard. 目前,在情节提要中使用UICollectionViewCells时,总是出现错误。 No other Controls show this behavior. 没有其他控件显示此行为。 How can I get rid of them? 我该如何摆脱它们?

故事板错误

This is how one of the affected CollectionViewCells looks like: 这是受影响的CollectionViewCells之一的样子:

在此处输入图片说明

This is how I defined it: 这是我的定义方式:

UICollectionViewCell定义

Here is the code of the CategoryCollectionCell 这是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 }
    }
}

And this is the code of the CollectionViewController: 这是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)
    }
}

Ok. 好。 I found that the errors displayed by XCode had nothing to do with the actual problem. 我发现XCode显示的错误与实际问题无关。

The directory /Users/{Username}/Library/Logs/DiagnosticReports should contain files with names like this: IBDesignablesAgentCocoaTouch[...].crash 目录/Users/{Username}/Library/Logs/DiagnosticReports应该包含名称如下的文件: IBDesignablesAgentCocoaTouch[...].crash

Inside them I found stacktraces which led me to the real problem: The problem lied inside the code of a custom UITableViewCell instead of a UICollectionViewCell 在它们内部,我发现了导致我真正问题的UICollectionViewCell :该问题在于自定义UITableViewCell而不是UICollectionViewCell的代码内

class FooTableCell : UITableViewCell {

    @IBOutlet private weak var checkmarkImageView: UIImageView!

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

The checkmarkImageView was nil when using the designer. 使用设计器时, checkmarkImageViewnil Because of this the Cocoa Storyboard Agent crashed. 因此,可可情节提要代理崩溃了。

I fixed it by adding a guard statement: 我通过添加保护声明来解决此问题:

class FooTableCell : UITableViewCell {

    @IBOutlet private weak var checkmarkImageView: UIImageView!

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

            imageView.hidden = !selected
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM