为什么UICollectionViewCell的出口是零? [英] Why is UICollectionViewCell's outlet nil?
问题描述
我在Interface Builder中创建了一个自定义UICollectionViewCell,将它绑定到类上,然后当我想使用并将字符串设置为字符串上的标签时,tha label的值为nil。
I have created a custom UICollectionViewCell in Interface Builder, binded views on it to the class, and then when I want to use and set a string to the label on the string, tha label has a nil value.
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")
}
override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
var cell: LeftMenuCollectionViewCell
cell = collectionView.dequeueReusableCellWithReuseIdentifier("ls", forIndexPath: indexPath) as LeftMenuCollectionViewCell
println(cell.label) // <- this is nil, why??
cell.label.text = "asd"
return cell
}
和子类化单元格:
class LeftMenuCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
}
推荐答案
我打电话给 self.collectionView.registerClass(LeftMenuCollectionViewCell.self,forCellWithReuseIdentifier:ls)
再次。 如果您使用的是故事板,则不希望将其称为。它会覆盖你的故事板中的内容。
I am calling self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")
again. If you are using a storyboard you don't want to call this. It will overwrite what you have in your storyboard.
如果你还有问题,请检查 reuseIdentifier
是否中的 c> dequeueReusableCellWithReuseIdentifier 和 storyboard
。
If you still have the problem check wether reuseIdentifier
is same in dequeueReusableCellWithReuseIdentifier
and in storyboard
.
这篇关于为什么UICollectionViewCell的出口是零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!