use*_*776 5 uitableview ios swift
我在表格视图单元格内有一个按钮(如下图所示),当我单击该按钮时,
didSelectRowAt 索引路径
没有被触发,有人可以建议我如何做到这一点吗?
请注意: 我正在单击按钮执行一组操作,此外我还想
didselectRowAt indexPath
被触发。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Table view cell has been clicked")
}
Run Code Online (Sandbox Code Playgroud)
将出口放置到自定义单元类内的按钮。然后在 cellForItemAt 中设置标签并添加按钮选择器。这个例子是针对collectionView的,只是改变以适应tableView。
如果您希望每个单元格中都有按钮,则必须这样做。将静态按钮添加到单个单元格不会调用 didSelectItemAt,因为您点击的按钮没有引用可重用单元格的索引路径。
这样我们就可以将button.tag发送给函数,这样我们就知道按钮与哪个单元格相关。
class MyClassViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
.... // Stuff
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
....// Create Cell
cell.deleteCellButton.tag = indexPath.item
cell.deleteCellButton.addTarget(self, action: #selector(MyClassViewController.deleteCellButtonTapped(_:)), for: .touchUpInside)
return cell
}
func deleteCellButtonTapped(_ sender: Any) {
... // Stuff
print("Selector called")
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4010 次 |
| 最近记录: |