2015-07-10 56 views
0

我创建了UICollectionReusableView的子类。然后在功能将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 

它崩溃在运行时的“让景......”与此错误:

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) { 
... 
} 

我很困惑,为什么我不能做这种低调。这与我使用DR ...课程的外部库有什么关系吗?

+1

您不能倒下,因为对象不是这种类型 - 正如例外说的那样。与您指定的重用标识符关联的类是“UICollectionReusableView”,而不是您的子类。 – Paulw11

+0

@ Paulw11谢谢。这表明我朝着正确的方向前进。在我构建子类之后,我忘记了更改registerClass语句。现在全部修好了。 – tangobango

回答

0

感谢Paulw11,我想出了该怎么做。我将把这个答案放在其他任何试图在没有子类的情况下构建应用程序的子类的菜鸟。

建立子类后,我需要从改变我的registerClass声明:

collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: DRCollectionViewTableLayoutSupplementaryViewRowHeader, withReuseIdentifier: collectionViewHeaderIdentifier) 

collectionView.registerClass(CVHeaderView.self, forSupplementaryViewOfKind: DRCollectionViewTableLayoutSupplementaryViewRowHeader, withReuseIdentifier: collectionViewHeaderIdentifier) 

所有固定。