简体   繁体   English

如何在表格视图单元格中添加自定义UIButton?

[英]How to add custom UIButton in table view cell?

I have a custom UIButton that I need to add to my dynamic table view cell. 我有一个自定义UIButton ,需要将其添加到动态表视图单元格中。 None of the tutorials I've used has worked for me. 我使用的所有教程都没有对我有用。 My UIButton only appears when the cell is clicked, then quickly vanishes. 我的UIButton仅在单击单元格时出现,然后迅速消失。 When I click the button the app crashes and I get ...ViewController addButtonPressed]: unrecognized selector sent to instance . 当我单击按钮时,应用程序崩溃,我得到...ViewController addButtonPressed]: unrecognized selector sent to instance What do I need to do here? 我在这里需要做什么?

class TableViewCell: UITableViewCell {

        @IBOutlet weak var addFriendButton: UIButton!

}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBAction func addButtonPressed(sender: UIButton!) {
            println("Hey")
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            var friendsCell = tableView.dequeueReusableCellWithIdentifier("AddFriendsCell", forIndexPath: indexPath) as! TableViewCell

            friendsCell.addFriendButton.tag = indexPath.row

            friendsCell.addFriendButton.addTarget(self, action: "addButtonPressed", forControlEvents: .TouchUpInside)

            return friendsCell

        }
}

在此处输入图片说明

try this: (add ' : ' in your selector name) 尝试以下操作:(在选择器名称中添加' ')

friendsCell.addFriendButton.addTarget(self, action: "addButtonPressed:", forControlEvents: .TouchUpInside)

I hope this will solve your problem of un recognised selector. 我希望这能解决您无法识别选择器的问题。

I am a bit confuse, the function below should not be an outlet: 我有点困惑,下面的功能不应该是插座:

@IBAction func addButtonPressed(sender: UIButton!) {
            println("Hey")
}

You should have a button in the TableViewCell and just a method in the ViewController, are they supposed to be the same button? 您应该在TableViewCell中有一个按钮,而在ViewController中只有一个方法,它们应该是同一按钮吗?

If so remove the bottom from the ViewController and its outlet and leave just the function to be called. 如果是这样,请从ViewController及其插座中移除底部,仅保留要调用的函数。

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

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