把出口到按钮的自定义类细胞内。然后在cellForItemAt中为button设置标签并添加选择器。这个例子是为colletionView只是改变以适应tableView。
如果你想在每个单元格中的按钮,这是你如何做到这一点。向单个单元添加一个静态按钮不会调用didSelectItemAt,因为您点击的按钮没有引用可重用单元indexPath。
这样我们发送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")
}
}
您是否将TableView委托分配给您要放入代码的类?你应该使用UITableViewDelegate –
按钮的功能是什么?它可以直接调用'didselectRowAt indexPath'(或者更准确地说,调用'didselectRowAt indexPath'调用的其他函数以及按钮自己的方法)。看看:http://stackoverflow.com/questions/20117163/didselectrowatindexpath-is-not-invoked-to-uibutton-of-cellforrowatindex –
可能重复的[didSelectRowAtIndexPath不被调用到cellForRowAtIndex的UIButton](http:///stackoverflow.com/questions/20117163/didselectrowatindexdex-is-not-invoked-to-uibutton-of-cellforrowatindex) – 2017-02-27 01:23:33