简体   繁体   English

如何在iOS中的表格视图单元格中添加按钮?

[英]How to add a button to a table view cell in iOS?

I'm creating a productivity app in Swift. 我正在Swift中创建一个生产力应用程序。 I'm not using a prototype cell in the Storyboard as most of it has been written in code already. 我没有在情节提要中使用原型单元,因为大多数单元已经用代码编写。 I'd like to a checkbox button. 我想要一个复选框按钮。 How would I go about doing that? 我将如何去做?

While the answer from Tim is technically correct, I would not advise on doing this. 尽管蒂姆的答案在技术上是正确的,但我不建议这样做。 Because the UITableView uses a dequeuing mechanism, you could actually receive a reused cell which already has a button on it (because you added it earlier). 因为UITableView使用一种出队机制,所以您实际上可能会收到一个已经有一个按钮的重用单元格(因为您之前添加了它)。 So your code is actually adding a 2nd button to it (and a 3rd, 4th, etc). 因此,您的代码实际上是向其中添加了第二个按钮(以及第三个,第四个等)。

What you want to do, is create a subclass from the UITableViewCell which adds a button to itself, while it is being instantiated. 您想要做的是从UITableViewCell创建一个子类,该子类在实例化时向其自身添加一个按钮。 Then you can just dequeue that cell from your UITableView and it will automatically have your button on it, without the need to do it in the cellForRowAtIndexPath method. 然后,您可以从UITableView中将该单元出队,它将自动在其上具有按钮,而无需在cellForRowAtIndexPath方法中进行操作。

Something like this: 像这样:

class MyCustomCellWithButton: UITableViewCell {

    var clickButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton;

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier);

        self.contentView.addSubview(self.clickButton);

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

And then you can actually dequeue it in the cellForRowAtIndexPath like this. 然后您实际上可以像这样在cellForRowAtIndexPath使它出队。

var cell = tableView.dequeueReusableCellWithIdentifier("my-cell-identifier") as? MyCustomCellWithButton;
if (cell == nil) {
    cell = MyCustomCellWithButton(style: UITableViewCellStyle.Default, reuseIdentifier: "my-cell-identifier");
}       
return cell!;

Well, first of all your cellForRowAtIndexPath should probably use the dequeue mechanism so you aren't recreating cells every time when virtualizing. 好吧,首先,您的cellForRowAtIndexPath应该可能使用出队机制,这样您就不必在每次虚拟化时都重新创建单元。

But that aside, all you need to do is create the button, and add it as a sub view to the cell. 除此之外,您所需要做的就是创建按钮,并将其作为子视图添加到单元格中。

cell.addSubview(newButton)

But of course then you will have to manage the sizing and layout as appropriate. 但是,当然,您将不得不适当地管理大小和布局。

A UITableViewCell also has a selected state and a didSelect and didDeselect method available that listens to taps on the whole cell. UITableViewCell也具有选定状态,并且有一个didSelectdidDeselect方法可用,该方法侦听整个单元格上的拍子 Perhaps that's a bit more practical since you seem to want to check/uncheck checkboxes, which is more or less the same as selecting. 也许这有点实用,因为您似乎想选中/取消选中复选框,这与选择大致相同。 You could set the cell in selected state right after you dequeued it. 您可以在将单元出队后立即将其设置为选定状态。

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

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