解決したいこと
- 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}
なにか思いつく方いらっしゃいましたらよろしくおねがいします。
回答1件
あなたの回答
tips
プレビュー