我创建了UICollectionReusableView的一个子类。然后在函数中
collectionViewTableLayoutManager(manager: collectionView: headerViewForRow row: indexPath: )我正在尝试将一个视图出队并将其强制转换为子类。
下面是该方法的开始:
func collectionViewTableLayoutManager(manager: DRCollectionViewTableLayoutManager!, collectionView: UICollectionView!, headerViewForRow row: UInt, indexPath: NSIndexPath!) -> UICollectionReusableView! {
let view = collectionView.dequeueReusableSupplementaryViewOfKind(DRCollectionViewTableLayoutSupplementaryViewRowHeader, withReuseIdentifier: collectionViewHeaderIdentifier, forIndexPath: indexPath) as! CVHeaderView它在运行时崩溃在"let view ...“出现以下错误:
Could not cast value of type 'UICollectionReusableView' (0x103994fb8) to 'CollectionViewTableLayout.CVHeaderView' (0x1023f3bd0).下面是我的子类代码:
class CVHeaderView: UICollectionReusableView {
let textLabel: UILabel!
override init(frame: CGRect) {
let textSize = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
textLabel = UILabel(frame: textSize)
super.init(frame: frame)
textLabel.font = UIFont.systemFontOfSize(10)
textLabel.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
textLabel.textAlignment = NSTextAlignment.Center
textLabel.backgroundColor = UIColor.clearColor()
self.addSubview(textLabel)
}
required init(coder aDecoder: NSCoder) {
...
}我不明白为什么我不能这样做。是不是跟我用了博士的外部库有关…类?
发布于 2015-07-11 06:13:41
多亏了Paulw11,我知道该怎么做了。我将把答案放在这里,供任何其他新手在没有子类的情况下构建应用程序后尝试子类。
在构建了子类之后,我需要将我的registerClass语句从:
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: DRCollectionViewTableLayoutSupplementaryViewRowHeader, withReuseIdentifier: collectionViewHeaderIdentifier)至
collectionView.registerClass(CVHeaderView.self, forSupplementaryViewOfKind: DRCollectionViewTableLayoutSupplementaryViewRowHeader, withReuseIdentifier: collectionViewHeaderIdentifier)都修好了。
https://stackoverflow.com/questions/31350538
复制相似问题