Ios 从Swift 3中的自定义UITableViewCell检测UIButton

Ios 从Swift 3中的自定义UITableViewCell检测UIButton,ios,swift,xcode,Ios,Swift,Xcode,我有一个自定义UITableViewCell,在其中我使用Interface Builder连接了我的UIButton @IBOutlet var myButton: UIButton! 在UITableViewController的单元配置下,我有以下代码: override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cust

我有一个自定义UITableViewCell,在其中我使用Interface Builder连接了我的UIButton

@IBOutlet var myButton: UIButton!
在UITableViewController的单元配置下,我有以下代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var customCell = tableView.dequeueReusableCell(withIdentifier: self.MY_CELL_IDENTIFIER, for: indexPath) as! myCustomCell

// CONFIGURE OTHER CELL PARAMETERS

customCell.myButton.tag = indexPath.row

customCell.myButton.addTarget(self, action: #selector(myButtonPressed), for: UIControlEvents.touchUpInside)

return customCell

}
最后,我有

private func myButtonPressed(sender: UIButton!) {
        let row = sender.tag
        print("Button Sender row: \(row)")            
    }
此代码不起作用,除非我将函数定义更改为以下内容:

@objc private func myButtonPressed(sender: UIButton!) {
            let row = sender.tag
            print("Button Sender row: \(row)")

        }

是否有更好的方法在Swift 3中的自定义UITableViewCell上实现UIButton

您也可以使用代理执行相同的操作

在自定义UITableViewCell类中直接映射IBAction of button


在viewcontroller中实现委托方法,并在按钮操作上从自定义单元格调用委托方法。

您也可以使用委托执行相同的操作

在自定义UITableViewCell类中直接映射IBAction of button


在viewcontroller中实现委托方法,并在按钮操作上从自定义单元格调用委托方法。

我不太喜欢使用view
标记。与此相反,您可以使用代理模式在按下按钮时通知
viewController

protocol CustomCellDelegate: class {
    func customCell(_ cell: UITableViewCell, didPressButton: UIButton)
}

class CustomCell: UITableViewCell {
    // Create a delegate instance
    weak var delegate: CustomCellDelegate?

    @IBAction func handleButtonPress(sender: UIButton) { 
        self.delegate?.customCell(self, didPressButton: sender)
    }
}

class ViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var customCell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! CustomCell
        // CONFIGURE OTHER CELL PARAMETERS

        //Assign the cell's delegate to yourself
        customCell.delegate = self
        return customCell
    }
}

extension ViewController: CustomCellDelegate {
    // You get notified by the cell instance and the button when it was pressed
    func customCell(_ cell: UITableViewCell, didPressButton: UIButton) {
        // Get the indexPath
        let indexPath = tableView.indexPath(for: cell) 
    }
}

我不太喜欢使用view
tag
s。与此相反,您可以使用代理模式在按下按钮时通知
viewController

protocol CustomCellDelegate: class {
    func customCell(_ cell: UITableViewCell, didPressButton: UIButton)
}

class CustomCell: UITableViewCell {
    // Create a delegate instance
    weak var delegate: CustomCellDelegate?

    @IBAction func handleButtonPress(sender: UIButton) { 
        self.delegate?.customCell(self, didPressButton: sender)
    }
}

class ViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var customCell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! CustomCell
        // CONFIGURE OTHER CELL PARAMETERS

        //Assign the cell's delegate to yourself
        customCell.delegate = self
        return customCell
    }
}

extension ViewController: CustomCellDelegate {
    // You get notified by the cell instance and the button when it was pressed
    func customCell(_ cell: UITableViewCell, didPressButton: UIButton) {
        // Get the indexPath
        let indexPath = tableView.indexPath(for: cell) 
    }
}

是的,有一种更聪明、更好的方法可以做到这一点。您的方法的主要问题是,它仅在没有执行插入、删除或移动单元格操作时才起作用。因为这些操作中的任何一个都可以更改为不同indexPath创建的单元格的de indexPath

我使用的系统是:

1.-在单元格类MyCustomCell中创建一个iAction(大写为M。它是一个类,因此请正确命名)

2.-将按钮连接到该动作

3.-至少使用方法声明协议MyCustomCellDelegate

func myCustomCellButtonAction(_ cell:MyCustomCell)
并将属性添加到MyCustomCell

 var delegate : MyCustomCellDelegate?    
4.-将视图控制器设置为MyCustomCellDelegate

在连接到按钮的MyCustomCell方法中,调用委托方法:

 delegate?.myCustomCellButtonAction( self )
5.-在视图控制器中,在方法cellForRowAt中:

 customCell.delegate = self
6.-在视图控制器中,在方法myCustomCellButtonAction中:

func myCustomCellButtonAction( _ cell: MyCustomCell ) {
    let indexPath = self.tableView.indexPathForCell( cell )

    // ......  continue .....
}

是的,有一种更聪明、更好的方法可以做到这一点。您的方法的主要问题是,它仅在没有执行插入、删除或移动单元格操作时才起作用。因为这些操作中的任何一个都可以更改为不同indexPath创建的单元格的de indexPath

我使用的系统是:

1.-在单元格类MyCustomCell中创建一个iAction(大写为M。它是一个类,因此请正确命名)

2.-将按钮连接到该动作

3.-至少使用方法声明协议MyCustomCellDelegate

func myCustomCellButtonAction(_ cell:MyCustomCell)
并将属性添加到MyCustomCell

 var delegate : MyCustomCellDelegate?    
4.-将视图控制器设置为MyCustomCellDelegate

在连接到按钮的MyCustomCell方法中,调用委托方法:

 delegate?.myCustomCellButtonAction( self )
5.-在视图控制器中,在方法cellForRowAt中:

 customCell.delegate = self
6.-在视图控制器中,在方法myCustomCellButtonAction中:

func myCustomCellButtonAction( _ cell: MyCustomCell ) {
    let indexPath = self.tableView.indexPathForCell( cell )

    // ......  continue .....
}

您是否尝试过从界面生成器创建操作?放弃私有keyword@LopesFigueiredo-我没有创建iAction的原因是因为将有多行,我必须检测按钮是从哪一行按下的。每次重复使用单元格时,您都会向每个按钮添加目标。如果不希望在触摸按钮时多次调用myButtonPressed方法,则只需添加一次target。@LopesFigueRedo-谢谢。我知道如何创建iAction,但要从行中唯一标识每个UIButton,我尝试了如上所述的方法。您是否尝试过从界面生成器创建操作?放弃私有keyword@LopesFigueiredo-我没有创建iAction的原因是因为将有多行,我必须检测按钮是从哪一行按下的。每次重复使用一个iAction时,您都会向每个按钮添加目标牢房。如果不希望在触摸按钮时多次调用myButtonPressed方法,则只需添加一次target。@LopesFigueRedo-谢谢。我知道如何创建iAction,但为了从行中唯一标识每个UIButton,我尝试了上面详述的方法。