Swift, collection view & storyboard, get null when using viewWithTag on another Label

一笑奈何 提交于 2020-01-06 18:43:09

问题


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
}

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"

I get the following error (on the "let" line):

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

The current message is shown in the source code: "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, sub code =0x0)"

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.

I am using Xcode 6 beta 5.

Any help will be greatly appreciated, I do not know where to look for more information.

Thank you / Martin


回答1:


Okay, so it seems that asking a question here makes me find the answer.. I do not know why it worked, but it did.

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.



来源:https://stackoverflow.com/questions/25534655/swift-collection-view-storyboard-get-null-when-using-viewwithtag-on-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!