我目前在 Storyboard 中使用 UICollectionViewCells 时总是出错。没有其他控件显示此行为。我怎样才能摆脱它们?
这是受影响的 CollectionViewCells 之一的样子:
我是这样定义的:
这是 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
}
}
}
使用设计器时,checkmarkImageView
为nil
。因此,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/