私のアプリでは、UICollectionViewを使用しています。コレクションビューの任意のセルをクリックすると、UIAlertControllerを開発したいと思います。 私は次のコードで開始:Swift:UICollectionViewのセルをクリックし、AlertViewControllerを開きます。
extension HomeViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
…
}
// specify cells
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
….
}
// called when widget is moved
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
…
}
// called when clicked
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Got clicked!")
}
}
しかし、どういうわけか、 "クリックされました!"決して印刷されません。
デリゲートとデータソースを設定しましたか? – ronatory
いいえ、どうすればいいですか?申し訳ありません、私は初心者です:D –
@ AlexanderJeitler-Stehr、あなたがiOSを始め、Swiftを学んだことを知ってうれしいです。エクステンションに** UICollectionViewDelegate **を追加することができません。 ** UICollectionViewDataSource **の後に追加するだけでいいです。デリゲートを 'HomeViewController'にバインドしたことを確認してください。ハッピーコーディング:) –