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

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

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

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

Swift

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

Q&A

0回答

584閲覧

collectionViewCellについて2

Ytan

総合スコア39

Xcode

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

Swift

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

0グッド

1クリップ

投稿2020/03/16 03:03

編集2020/03/16 05:30

Swift

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 var firstFlippedCardIndex:IndexPath? 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 // Do any additional setup after loading the view. 16 cardArray = model.getCards() 17 18 collectionView.delegate = self 19 collectionView.dataSource = self 20 21 //Call getcard method of cardModel 22 23 } 24 25 override func didReceiveMemoryWarning() { 26 super.didReceiveMemoryWarning() 27 } 28 29 //section目のセクションにいくつ行があるかを返す 30 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 31 32 return cardArray.count 33 } 34 35 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 36 37 // print(#function) 38 39 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath) as! CardCollectionViewCell 40 41 //get the card that collection view is trying to display 42 let card = cardArray[indexPath.row] 43 44 cell.setCard(card) 45 46 return cell 47 } 48 49 50 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 51 52 53 print(#function) 54 55 //get the cell that the user selected 56 let cell = collectionView.cellForItem(at: indexPath) as! CardCollectionViewCell 57 58 //get the card that userselected 59 let card = cardArray[indexPath.row] 60 61 if card.isFlipped == false{ 62 63 //Flip the Card 64 cell.flip() 65 66 card.isFlipped = true 67 68 }else{ 69 // 70 // cell.flipBack() 71 // 72 // card.isFlipped = false 73 74 } 75 76 77 } 78 79 80 81}

Swift

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

問題のセル
問題のセル

実現したいセル
実現したいセル

前の質問で動かなかったものを新規で立ち上げコピペしました。
しかしFlipはされたのですが、collectionViewCelがStandardのままです。
解決策はありますか?

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

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

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

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

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

TsukubaDepot

2020/03/16 03:14

collectionViewCell が Standard のまま、という表現がよく分かりません。 具体的にはどのような状態になっているのでしょうか。
Ytan

2020/03/16 03:35

上部にセルの大きさが変化せず小さいまま16個固まっていて分散されないです。
TsukubaDepot

2020/03/16 04:01

CollectionViewCell が同じ場所に重なっていて表示されない、という意味でしょうか。 シミュレータなどの画面をキャプチャして貼り付けてもらえると分かりやすいと思います。 また、この質問のタイトルも「UICollectionViewCell が重なって表示される」など、他の方がみて「何が問題なのか」明確にわかるようなタイトルにするのが良いかと思います。 また、CollectionViewCellの設定についても、以下のページなどで動的に設定する方法がありますから、これらも参考にされてはいかがでしょうか。 https://qiita.com/takehilo/items/d0e56f88a42fb8ed1185
Ytan

2020/03/16 05:27 編集

返信に画像を貼ることはどのようにするのでしょうか? 質問や自己解決のフォームでは可能なのですが、返信のフォームではイマイチわからないです。
TsukubaDepot

2020/03/16 05:26

画像は追記には貼れないので、質問本文に追記していただけませんか?
Ytan

2020/03/16 05:31

そうなのですね、追加しました。
TsukubaDepot

2020/03/16 05:46

分散されないというのは、たとえばセルが16個あって、それを均等に画面に広げたい、ということでしょうか。
Ytan

2020/03/16 05:49

そうですね、前の質問のコードでは上手くいったのですか、なぜかコピペしたところ1枚目のようになってしまいました。エラーは出ておりません。 またidentifierも設定されております。
TsukubaDepot

2020/03/16 05:54 編集

コードで設定していないのであれば、おそらくInterface Builderでセルの高さや幅、制約を設定していたのではないでしょうか。 以前の設定と今の設定について、まずは設定をいじらず、一つひとつ見比べてみてはいかがでしょうか。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問