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

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

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

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

UI

UIはUser Interfaceの略であり、人間がコンピュータとやりとりをするためのシステムです。

Swift

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

Q&A

解決済

1回答

1039閲覧

【Swift】UICollectionViewのdelegateメソッドが実行されない

popai306

総合スコア157

iOS

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

UI

UIはUser Interfaceの略であり、人間がコンピュータとやりとりをするためのシステムです。

Swift

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

0グッド

0クリップ

投稿2020/01/12 09:20

解決したいこと

  • ViewController内に3つのUICollectionViewがあり、3つともViewController内でdelegateをselfに設定しているにも関わらず、そのうち一つのUICollectionViewのdelegateメソッドが実行されず、そのUICollectionViewを描画できない

ソースでの解説

  • 全量だと冗長なので一部抜粋して貼り付けます
  • 上記3つのUICollectionViewのうちの一つ、を指すのがfeededSceneCollectionViewです

ViewController

1override func loadView() { 2 view = MyProfileView(tabbarHeight: self.tabBarController?.tabBar.frame.height ?? 0) 3} 4 5// ここで3つUICollectionViewのdelegateを設定 6override func viewDidLoad() { 7 mainView.profileImage.addGestureRecognizer( 8 UITapGestureRecognizer( target: self, action: #selector(didTapProfileIcon))) 9 mainView.profileEditButton.addTarget(self, action: #selector(didTapProfileEditButton), for: .touchUpInside) 10 11 mainView.datingCollectionView.dataSource = self 12 mainView.datingCollectionView.delegate = self 13 14 mainView.feededSceneCollectionView.dataSource = self 15 mainView.feededSceneCollectionView.delegate = self 16 17 mainView.sceneCollectionView.dataSource = self 18 mainView.sceneCollectionView.delegate = self 19} 20 21extension MyProfileViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { 22 // DataSourceはちゃんと設定できていることを確認済み(return 5)の部分 23 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 24 if collectionView == mainView.datingCollectionView { 25 return 3 26 } else if collectionView == mainView.feededSceneCollectionView { 27 return 5 28 } else { 29 // カメラアイコン分を+1する 30 return (user?.scenes?.count ?? 0) + 1 31 } 32 } 33 34 // このdelegateメソッド内でfeededSceneCollectionViewが呼ばれない>_< 35 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 36 if collectionView == mainView.datingCollectionView { 37 // My Dating 38 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: DatingCell.reuseIdentifier, for: indexPath) as! DatingCell 39 if let datings = user?.datings, datings.count > indexPath.row { 40 cell.configure(mode: .dating, dating: datings[indexPath.row]) 41 } else if user?.datings?.count ?? 0 == indexPath.row { 42 cell.configure(mode: .addObject) 43 } else{ 44 cell.configure(mode: .empty) 45 } 46 return cell 47 } else if collectionView == mainView.feededSceneCollectionView { 48 // Feeded Scene 49 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FeededSceneCell.reuseIdentifier, for: indexPath) as! FeededSceneCell 50 if let feededScenes = user?.feededScenes, feededScenes.count > indexPath.row { 51 cell.configure(mode: .scene, scene: feededScenes[indexPath.row]) 52 } else { 53 cell.configure(mode: .empty) 54 } 55 return cell 56 } else { 57 // My Scene 58 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SceneCell.reuseIdentifier, for: indexPath) as! SceneCell 59 if let scenes = user?.scenes { 60 if indexPath.row == 0 { 61 cell.configure(mode: .addObject) 62 } else { 63 cell.configure(mode: .scene, scene: scenes[indexPath.row - 1]) 64 } 65 } 66 return cell 67 } 68 } 69}

なにか思いつく方いらっしゃいましたらよろしくおねがいします。

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

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

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

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

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

akaakoz

2020/01/12 10:47

Extenstionのところ、UICollectionViewDelegate追加するとどうなります?
popai306

2020/01/12 10:54

ご記入ありがとうございます、暫定的には自己解決致しました。
guest

回答1

0

自己解決

feededSceneCollectionViewで用いているfeededSceneflowLayoutの以下のプロパティを消したら表示されるようになりました。

feededSceneflowLayout.scrollDirection = .horizontal sceneCollectionView.isScrollEnabled = false

投稿2020/01/12 10:53

popai306

総合スコア157

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問