Swift,集合视图&故事板,在另一个标签上使用 viewWithTag 时获得 null [英] Swift, collection view & storyboard, get null when using viewWithTag on another Label
问题描述
我看到了一些非常奇怪的行为,我需要一些帮助.我有一个最初有一个标签的集合视图单元格,标签为 43.
I am seeing some extremely odd behavior that I would like some assistance with. I have a collection view cell that originally had one label, with a tag of 43.
我正在使用以下代码设置标签的值:
I am setting the value of the label using the following code:
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("playerCell", forIndexPath: indexPath) as UICollectionViewCell
cell.backgroundColor = UIColor.orangeColor()
let mintmplabel : UILabel = cell.viewWithTag(43) as UILabel
mintmplabel.text = "fruktkaka"
//(cell.viewWithTag(43) as UILabel).text = object.valueForKey("playerName").description
//let mintmplabelb : UILabel = cell.viewWithTag(41) as UILabel
//mintmplabelb.text = "megaman"
//(cell.viewWithTag(41) as UILabel).text = object.valueForKey("playerName").description
return cell
}
它按预期工作!但是,我后来尝试在集合视图单元格中添加另一个标签,并将其标记为 41.当我取消注释行时
It works as expected! However, I later tried to add another label to the collection view cell, and give it a tag of 41. When I uncomment the lines
//let mintmplabelb : UILabel = cell.viewWithTag(41) as UILabel
//mintmplabelb.text = "megaman"
我收到以下错误(在let"行):
I get the following error (on the "let" line):
致命错误:在展开可选值时意外发现 nil(lldb)
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
源代码中显示当前消息:Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, sub code =0x0)"
The current message is shown in the source code: "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, sub code =0x0)"
我尝试了许多不同的方法,但我就是无法让它发挥作用.我还尝试删除两个标签,并重新添加它们(使用相同的标签).这打破了标签 43 和 41 的标签!
I have tried many different approaches, but I just can not get it to work. I have also tried to remove both labels, and re-add them (with the same tags). That breaks both the label with tag 43 and 41!
要么我在如何使用标签方面遗漏了一些东西,要么我偶然发现了某种错误.
Either I am missing something on how to use tags, or I have stumbled upon some kind of bug.
我使用的是 Xcode 6 beta 5.
I am using Xcode 6 beta 5.
任何帮助将不胜感激,我不知道去哪里寻找更多信息.
Any help will be greatly appreciated, I do not know where to look for more information.
谢谢/马丁
推荐答案
好吧,看来在这里提出问题让我找到了答案.. 我不知道它为什么有效,但确实有效.
Okay, so it seems that asking a question here makes me find the answer.. I do not know why it worked, but it did.
我选择了无效的标签,在故事板属性检查器"中,未选中已安装"复选框,并选中了已安装 wC hR"复选框.通过取消选中已安装 wC hR"并选中已安装"复选框,它又开始工作了.
I selected the label that did not work, and in the Storyboard "attribute inspector", a checkbox that said "Installed" was unchecked, and underneath a checkbox that said "wC hR Installed" was checked. By unchecking the "wC hR installed" and checking the "installed" checkbox, it began to work again.
我不知道这是做什么的,如果我发现会更新答案.
I have no idea what this does, will update the answer if I find out.
这篇关于Swift,集合视图&故事板,在另一个标签上使用 viewWithTag 时获得 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!