。ボタンのindexPathが必要な場合は、サブクラスまたは他の手段でUIButtonのプロパティにする必要があります。その後、通常通りターゲットを追加
class ButtonWithIndexPath: UIButton {
var indexPath:IndexPath?
}
:
cell.showMapButton.addTarget(self, action: #selector(testFunc(button:)), for: .touchUpInside)
はそれにあなたのボタンのindexPathを設定するために忘れてはならない、それを行うための私の方法は、それが財産だとしてindexPathを持ってUIButtonのサブクラスを作成することです今までそれが
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! myCell
cell.button.indexPath = indexPath
...
return cell
}
であるセルと、それはindexPathを読み取るための機能でカスタムサブクラスですにそれを投げたの:
func textFunc(button: UIButton) {
let currentButton = (button as! ButtonWithIndexPath)
print(currentButton.indexPath)
}
デリゲートパターンまたはクロージャを使用できます。回答を見る[ここ](https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510# 38941510) – Paulw11
ターゲット/アクションパターンでカスタムパラメータを使用することはできません。サポートされている唯一の引数は、ボタンを送信するUI要素です。 – vadian