ボタンでUICollectionViewを作成しようとしています。ボタンのタイトルは設定できますが、ターゲットのアクションは機能しません。以下は私のコードです:UICollectionViewボタン
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TVButtonCell", for: indexPath) as! TVButtonCell
cell.channelButton.setTitle(channelKeys[indexPath.row], for: .normal)
cell.channelButton.addTarget(self, action: #selector(FirstViewController.pressed(_:)), for: .primaryActionTriggered)
return cell
}
func pressed(_ sender: AnyObject) {
print("Button pressed")
}
私は間違っていますか?
アプリがクラッシュしていますか?完全なスタックトレースを通過できますか? – nitinsh99
UICollectionViewセルにIBActionsを追加し、ViewControllerに代理人を追加することをお勧めします – Nathaniel
また、おそらくこれが役立ちます:http://stackoverflow.com/questions/29610316/how-to-add-a-delete-button-コレクションビュー - セル - イン - スイフト?rq = 1 – Nathaniel