首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将UICollectionReusableView向子类下转换失败

将UICollectionReusableView向子类下转换失败
EN

Stack Overflow用户
提问于 2015-07-11 05:26:32
回答 1查看 710关注 0票数 0

我创建了UICollectionReusableView的一个子类。然后在函数中

代码语言:javascript
复制
collectionViewTableLayoutManager(manager: collectionView: headerViewForRow row:  indexPath: )

我正在尝试将一个视图出队并将其强制转换为子类。

下面是该方法的开始:

代码语言:javascript
复制
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 ...“出现以下错误:

代码语言:javascript
复制
Could not cast value of type 'UICollectionReusableView' (0x103994fb8) to 'CollectionViewTableLayout.CVHeaderView' (0x1023f3bd0).

下面是我的子类代码:

代码语言:javascript
复制
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) {
...
}

我不明白为什么我不能这样做。是不是跟我用了博士的外部库有关…类?

EN

回答 1

Stack Overflow用户

发布于 2015-07-11 06:13:41

多亏了Paulw11,我知道该怎么做了。我将把答案放在这里,供任何其他新手在没有子类的情况下构建应用程序后尝试子类。

在构建了子类之后,我需要将我的registerClass语句从:

代码语言:javascript
复制
collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: DRCollectionViewTableLayoutSupplementaryViewRowHeader, withReuseIdentifier: collectionViewHeaderIdentifier)

代码语言:javascript
复制
collectionView.registerClass(CVHeaderView.self, forSupplementaryViewOfKind: DRCollectionViewTableLayoutSupplementaryViewRowHeader, withReuseIdentifier: collectionViewHeaderIdentifier)

都修好了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31350538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档