How to select a button in the tableview? [duplicat

2019-03-07 02:31发布

This question already has an answer here:

I can select row using the "didSelectRowAt" function in tableview of swift 3. But how do I do other actions when I select a button in a row? I do not know how to select a button in the tableview.

image

2条回答
劫难
2楼-- · 2019-03-07 02:33

Create an outlet action: And find the indexpath using this code

    @IBAction func memberPicTapped(_ sender:UIButton) {
            var superView = sender.superview
            while !(superView is UITableViewCell) {
                superView = superView?.superview
            }
            let cell = superView as! UITableViewCell
            if let indexpath = YourTableView.indexPath(for: cell){

            }
      }
查看更多
劳资没心,怎么记你
3楼-- · 2019-03-07 02:40

Create custom cell
Write below code inside cellForRowAt indexPath of UItableview Method

cell.buttonname?.addTarget(self, action: #selector(self.buttonmethod), for: .touchUpInside)
cell.buttonname.tag = indexPath.section

// Button Clicked Method

func buttonmethod(_ button: UIButton) {
     print(button.tag)
     // perform some action
}
查看更多
登录 后发表回答