Get section number in custom cell button action

后端 未结 5 1209
北恋
北恋 2021-01-13 23:42

I have a tableView dynamically populated with custom cells in several sections.

In my CustomCell class I have an @IBAction for a custom checkbox button in the cell.

5条回答
  •  臣服心动
    2021-01-14 00:06

    For find indexPath in Swift 4 and Swift 5 when calling button,

    // First you Add target on button in "cellForRowAt"

    cell.myBtn.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)
    

    // Add function

    func btnAction(_ sender: UIButton) {
        let point = sender.convert(CGPoint.zero, to: yourTableView as UIView)
        let indexPath: IndexPath! = yourTableView.indexPathForRow(at: point)
        print("indexPath.row is = \(indexPath.row) && indexPath.section is = \(indexPath.section)")
    }
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题