質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

584閲覧

collectionViewCellについて

Ytan

総合スコア39

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

1グッド

1クリップ

投稿2020/03/15 05:46

Swift5

1 2import UIKit 3 4class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource { 5 6 @IBOutlet weak var collectionView: UICollectionView! 7 8 var model = CardModel() 9 var cardArray = [Card]() 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 collectionView.delegate = self 15 collectionView.dataSource = self 16 17 //Call getcard method of cardModel 18 cardArray = model.getCards() 19 } 20 21 override func didReceiveMemoryWarning() { 22 super.didReceiveMemoryWarning() 23 } 24 25 //section目のセクションにいくつ行があるかを返す 26 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 27 28 return cardArray.count 29 } 30 31 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 32 33 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath) as! CardCollectionViewCell 34 35 //get the card that collection view is trying to display 36 let card = cardArray[indexPath.row] 37 38 cell.setCard(card) 39 40 return cell 41 } 42 43 44 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 45 46 //get the cell that the user selected 47 let cell = collectionView.cellForItem(at: indexPath) as! CardCollectionViewCell 48 49 //get the card that userselected 50 let card = cardArray[indexPath.row] 51 52 if card.isFlipped == false{ 53 54 //Flip the Card 55 cell.flip() 56 57 card.isFlipped = true 58 }else{ 59 60 cell.flipBack() 61 62 card.isFlipped = false 63 64 } 65 66 67 } 68 69}

swift

1 2import Foundation 3 4class CardModel{ 5 6 func getCards() -> [Card] { 7 8 //Declare an array to store the generate cards 9 var generatedCardsArray = [Card]() 10 11 //Randomaly generate pairs of card 12 for _ in 1...8{ 13 14 //getNumber 15 let randomNumber = arc4random_uniform(13) + 1 16 17 //Log the Number 18 print("The generated card Number(randomNumber)") 19 20 //CreateNumber 21 var cardOne = Card() 22 cardOne.imageName = "card(randomNumber)" 23 24 generatedCardsArray.append(cardOne) 25 26 //create secod 27 let cardTwo = Card() 28 cardTwo.imageName = "card(randomNumber)" 29 30 generatedCardsArray.append(cardTwo) 31 32 //Optinal: make it so we only have unipue paires of cords 33 34 35 } 36 37 //Todo: Randomize thr array 38 print(generatedCardsArray.count) 39 40 //Return the array 41 return generatedCardsArray 42 43 } 44 45 46}

Swift

1import UIKit 2 3class CardCollectionViewCell: UICollectionViewCell { 4 5 @IBOutlet weak var frontImageView: UIImageView! 6 7 @IBOutlet weak var backImageView: UIImageView! 8 9 var card:Card? 10 11 func setCard(_ card:Card){ 12 13 //keep track of the card that gets passed 14 self.card = card 15 16 frontImageView.image = UIImage(named: card.imageName) 17 } 18 19 func flip(){ 20 21 UIView.transition(from: backImageView, to: frontImageView, duration: 0.3, options: [.transitionFlipFromLeft, .showHideTransitionViews], completion: nil) 22 23 } 24 25 func flipBack(){ 26 27 UIView.transition(from: frontImageView, to: backImageView, duration: 0.3, options: [.transitionFlipFromRight, .showHideTransitionViews], completion: nil) 28 } 29} 30

カードを押すとこの段階でFlipしてくれるとのことなのですが、Flipされません。
エラーもなくどこで間違えているのか見当がつきません。
identifierも間違いなく設定させれてます。

s.k👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TsukubaDepot

2020/03/15 09:18

画像は表示されていますか? 同じコード+αで試してみていますが、こちらではきちんとフリップしています。 たとえば、 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { の先頭行にprint(#function)などと入れて実行し、セルを選択したときに関数名は表示されますでしょうか。
Ytan

2020/03/15 18:26 編集

コンソールの方にcollectionView(_:cellForItemAt:)と一様表示はされます。
TsukubaDepot

2020/03/15 23:36

collectionView(_:cellForItemAt:) はセルを表示するときに呼ばれる dataSource ですね。なので、ビューが表示されるときに呼ばれる関数です。 一方、collectionView(_:didSelectItemAt:) はセルをクリックしたときに呼ばれる delegate です。なので、セルをクリックしたときにこちらが呼ばれない(print(#func)の結果が表示されない)のであれば、どこかに問題があるのだと思います。
Ytan

2020/03/16 00:22

collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)のところに置いておりました。 collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) に直したところコンソールには何も表示されません。
TsukubaDepot

2020/03/16 01:01

Interface Builder でどこかの階層の User Interaction Enabled のチェックが外れている、ということはないでしょうか。 View, CollectionView, CardCell どれか一つでも外れていたらクリックできません。 https://www.l08084.com/entry/2016/05/29/175027
Ytan

2020/03/16 01:38

Interface BuilderのView controllerの中にある物は全てチェックはついてます。CollectionView, CardCellはこのMain.storyboardのInterface Builderの事ですか?
TsukubaDepot

2020/03/16 01:42

私が試した方法だと、Main.storyboard の View Controller に Collection View を配置し、その中に Collection View Cell を置き、Collection Reusable View の Identifire を Card Cell にリネームしています。
Ytan

2020/03/16 01:45

同じです。間違いはないですね。
Ytan

2020/03/16 01:49

collection view card cell content view と言った形です
TsukubaDepot

2020/03/16 01:52

再確認ですが、セルに画像は表示されていますでしょうか。 画像は表示されているのにクリックできないというのであれば、やっぱり何らかの設定を行なっている可能性を否定できません。 一度新規にプロジェクトを立ち上げ、ファイルをコピーしてoutletなどの接続を確認してみてはいかがでしょうか。
Ytan

2020/03/16 01:54

セル16個全てにflipする前に設定していた画像が表示されています。 分かりました!
Ytan

2020/03/16 03:05

新規で立ち上げてFlipはされたのですが、collectionViewCelのセルがStandardのままで上部に固定されたままです。 新しい質問を載せたので、よろしければ確認をお願いしたいです。
TsukubaDepot

2020/03/16 03:16 編集

もし、新規プロジェクトを作ることでクリックの問題がとりあえず解決したのであれば、このトピックは自己解決にしてはどうでしょうか。 新しい方にもコメントを入れました。
guest

回答1

0

自己解決

新規プロジェクトを立ち上げたところ上手く行きました。

投稿2020/03/16 03:36

Ytan

総合スコア39

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問