gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:55:35 24 4
gpt4 key购买 nike

我目前在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com