简体   繁体   English

如何使用Swift在UICollectionViewCell中设置UIButton的标题

[英]How to set the title of a UIButton in a UICollectionViewCell with Swift

I have a ViewController with a UICollectionView where I'd like to display the first two letters of the players in the users friend list, like this: 我有一个带有UICollectionView的ViewController,我想在其中显示用户朋友列表中玩家的前两个字母,如下所示:

func collectionView(collectionView: UICollectionView,
    cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        contactListCollection.registerClass(PlayerCell.self, forCellWithReuseIdentifier: "PlayerCell")

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PlayerCell", forIndexPath: indexPath) as! PlayerCell

        let contacts = contactList.getContactList() //Array of strings

        for(var i = 0; i < contacts.count; i++){

            var str = contacts[i]
            // First two letters
            let firstTwo = Range(start: str.startIndex,
                end: str.startIndex.advancedBy(2))

            str = str.substringWithRange(firstTwo)

            cell.setButtonTitle(str);
        }


        return cell;
}

func collectionView(collectionView: UICollectionView,
  numberOfItemsInSection section: Int) -> Int {

    return contactList.getContactList().count;
}

My PlayerCell Class is as follows: 我的PlayerCell类如下:

   class PlayerCell : UICollectionViewCell {

   @IBOutlet var button: UIButton?

   func setButtonTitle(text: String){
     button!.setTitle(text, forState: .Normal)
   }
}

When run the code it gives: Fatal error: unexpectedly found nil while unwrapping an Optional value 运行代码时,它给出: 致命错误:在展开可选值时意外发现nil

I found out that the button in my PlayerCell is nil 我发现PlayerCell中的按钮为nil

I have added the button inside my cell in the Storyboard and Connected those with referencing outlets 我在情节提要中的单元格中添加了按钮,并将这些按钮与参考插座相连

Am I missing something here? 我在这里想念什么吗?

Using xCode 7 with Swift 2.0 在Swift 2.0中使用xCode 7

As part of the compilation process, Xcode converts the storyboard to a collection of XIB files. 作为编译过程的一部分,Xcode将情节提要转换为XIB文件的集合。 One of those XIB files contains your cell design. 这些XIB文件之一包含您的单元格设计。

When your app loads the collection view controller that you designed in the storyboard, the controller takes care of registering the cell's XIB file for the cell. 当您的应用加载在情节提要中设计的集合视图控制器时,该控制器将为该单元注册该单元的XIB文件。

You are overwriting that registration by calling registerClass(_:forCellWithReuseIdentifier:) , severing the connection between the “PlayerCell” reuse identifier and the XIB containing the design of PlayerCell . 您将通过调用registerClass(_:forCellWithReuseIdentifier:)覆盖该注册,从而切断“ PlayerCell”重用标识符与包含PlayerCell设计的XIB之间的连接。

Get rid of this line: 摆脱这一行:

contactListCollection.registerClass(PlayerCell.self, forCellWithReuseIdentifier: "PlayerCell")

Also, make sure that in the storyboard you have set the cell's reuse identifier to "PlayerCell". 另外,请确保在情节提要中已将单元的重用标识符设置为“ PlayerCell”。

Try: 1. I would check that there isn't an old referencing outlet attached to the button. 尝试:1.我将检查按钮上没有旧的参照插座。 Right click on the button in the interface builder and ensure that only the appropriate outlet is still connected. 右键单击界面构建器中的按钮,并确保仅连接了适当的插座。

  1. Ensure that in the storyboard you have set the class of the re-useable cell to PlayerCell 确保在情节提要中已将可重复使用的单元格的类别设置为PlayerCell

  2. Ensure that you have Ctrl + dragged from the collection view to the view controller it is in and set the delegate and data source to itself. 确保已将Ctrl +从集合视图拖到其所在的视图控制器中,并将委托和数据源设置为其自身。

Hopefully, one of these may help you. 希望其中之一可能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM