如果我定义了一个UICollectionViewCell子类:
class TestCell<T>: UICollectionViewCell {
var float: CGFloat? = 3
var string = "abc"
}
注册:
collectionView.registerClass(TestCell<String>.self, forCellWithReuseIdentifier: "Test")
然后使用它:
collectionView.dequeueReusableCellWithReuseIdentifier("Test", forIndexPath: indexPath) as! TestCell<String>
我注意到应该初始化的属性有奇怪的行为。
为什么会发生这种情况,我该如何解决?
这是在Xcode 7.3
上答案 0 :(得分:1)
看起来这是与泛型相关的错误。
如果你摆脱了泛型说明符,变量会按照你的预期进行初始化。
如果需要通用说明符,可以通过在init方法中实例化属性而不是内联来解决问题:
class TestCell<T>: UICollectionViewCell {
override init(frame: CGRect) {
float = 3
string = "abc"
super.init(frame: frame)
}
var float: CGFloat?
var string: String
}