如何获取indexpath和indexpath.按下单元格中的按钮时



i在节中有一个桌面划分,当我点击单元格中的按钮时,我必须打印行数和截面。我该如何得到这个?

indexPath存储在您的单元格中,并在按下按钮时重新进行重复。

表视图控制器

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "custom") as! CustomTableCell
    cell.indexPath = indexPath
    return cell
}

自定义表单元

class CustomTableCell: UITableViewCell {
    var indexPath: IndexPath!
    @IBAction func buttonPressed() {
        print(indexPath)
    }
}

实现tableView:didSelectRowatIndExpath:方法。当它被调用时,将传递NSINDEXPATH,其中包含该部分和行属性。

每当按下表视图单元格时,此方法都会被称为。索引路径将是被按下的单元格的索引路径。

编辑:

我误解了这个问题。按下按钮时,通过访问按钮的监督来从按钮中获取tableViewCell。然后使用TableView的IndexPathForcell方法获取按钮的indexpath。

最新更新