ios 为什么 UICollectionViewCell 的出口为零?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25165195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is UICollectionViewCell's outlet nil?
提问by János
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.
我在 Interface Builder 中创建了一个自定义 UICollectionViewCell,将其上的视图绑定到类,然后当我想使用并将字符串设置为字符串上的标签时,标签的值为 nil。
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
}
And the subclassed cell:
和子类单元格:
class LeftMenuCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
}
回答by János
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.
我self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")
又来打电话了。如果您使用的是情节提要,则不想将其称为. 它将覆盖您在故事板中的内容。
If you still have the problem check wether reuseIdentifier
is samein dequeueReusableCellWithReuseIdentifier
andin storyboard
.
如果您仍然有问题请检查是否reuseIdentifier
是相同的dequeueReusableCellWithReuseIdentifier
,并在storyboard
。
回答by u1591907
Just remove this line:
只需删除此行:
self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")
回答by Vineeth
If you are using xib, make sure that you have added this line of code to your viewdidload.
如果您使用的是 xib,请确保您已将这行代码添加到您的 viewdidload。
Objective C:
目标 C:
[self.collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"MyCellIdentifier"];
Swift:
迅速:
collectionView.register(UINib(nibName:"MyCell", bundle: nil), forCellWithReuseIdentifier:"MyCellIdentifier")
回答by Michael
Gotta register that nib guys!
必须注册那些笔尖家伙!
collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "CustomCellId")
回答by Sergio
Looks like there's two ways to register and I was using the wrong one the first. I have a custom xib view so registered with the second option, and we have data!
看起来有两种注册方式,第一种我用错了。我有一个自定义的 xib 视图,所以注册了第二个选项,我们有数据!
1:
1:
collectionView?.register(YourItemClassName.self, forCellWithReuseIdentifier: "yourIdentifier")
2:
2:
collectionView?.register(UINib(nibName: "YourItemClassName", bundle: nil), forCellWithReuseIdentifier: "yourIdentifier")
回答by dgalluccio
I think that best solution is to directly use from storyboard where add a CollectionView
, in alternative you need to remove a CollectionViewCell
from your CollectionView
in storyboard and register a cell with the following command:
我认为最好的解决方案是直接使用 from storyboard where add a CollectionView
,或者你需要CollectionViewCell
从你的CollectionView
in storyboard 中删除 a并使用以下命令注册一个单元格:
collectionView?.register(UINib(nibName: "YourItemClassName", bundle: nil), forCellWithReuseIdentifier: "yourIdentifier")
collectionView?.register(UINib(nibName: "YourItemClassName", bundle: nil), forCellWithReuseIdentifier: "yourIdentifier")
回答by justRadojko
I had a similar problem, but my mistake was that I didn't delegate CollectionViewCell to be able to change the label text..
我遇到了类似的问题,但我的错误是我没有委托 CollectionViewCell 来更改标签文本。