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

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

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

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

Swift

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

Q&A

解決済

1回答

1018閲覧

didSelectされたカスタムセルのセルのAPIデータをViewControllerで扱う方法

Ytan

総合スコア39

Xcode

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

Swift

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

0グッド

0クリップ

投稿2021/01/03 13:03

編集2021/01/03 13:04

現状

TableView内にTableViewには4つのセクションにCollectionViewを設置してそれぞれに4つのAPIで
取得したImageやLabelを横スクロールで表示しています。

APIStructの構造体にそれぞれID(let id:Int)を持っております。

実現したいこと

CollectionViewでdidSelectしたCellのAPIのIDデータを
ViewControllerで取得して遷移とともに遷移先のViewに値を渡したいのですが
APIの表示が少し複雑なのでお力を貸していただきたいです。

コードで言うと
NewMovieViewControllerでdidSelect時にNewMovieTableViewCell内に配置してあるNewMovieCollectionViewCellのセルに登録されているlet id:Intの値をNewMovieViewControllerでregisterdMovieIDに代入してdidSelectItemAtのnextVCの遷移先のプロパティに値を乗せたいのですが

CustomCell内の4SectionでのCollectionViewのそれぞれのセルの値なので少し複雑な気がします。
NewMovieViewController内で構造体に準拠したプロパティを作りid,title,poster_pathにアクセスはできるのですが一つだけのセクションのみになってしまうので、あくまで押されたカスタムセルの値をViewControllerで扱う方法が知りたいです。

ソースコード

APIStruct

1import Foundation 2 3struct nowPlayingMovieStruct: Codable { 4 let results: [nowPlayingMovieItem] 5} 6 7struct nowPlayingMovieItem : Codable { 8 9 let id:Int 10 let title: String 11 let poster_path:String? 12 13} 14以下似たよう名前のStructが三つ同じ構図であります。

NewMovieCollectionViewCell

1import UIKit 2 3class NewMovieCollectionViewCell: UITableViewCell { 4 5 @IBOutlet var contentsCollectionView: UICollectionView! 6 @IBOutlet var titleLabel: UILabel! 7 8 var rowInTableViewCell: Int? 9 10 override func awakeFromNib() { 11 super.awakeFromNib() 12 // Initialization code 13 14 let nib = UINib(nibName: "NewMovieCollectionViewCell", bundle: .main) 15 contentsCollectionView.register(nib, forCellWithReuseIdentifier: "NewMovieCollectionViewCell") 16 } 17 18 override func setSelected(_ selected: Bool, animated: Bool) { 19 super.setSelected(selected, animated: animated) 20 21 // Configure the view for the selected state 22 } 23 24 func setCollectionViewDataSourceDelegate 25 <D: UICollectionViewDataSource & UICollectionViewDelegate> 26 (dataSourceDelegate: D, forRow row: Int) { 27 28 contentsCollectionView.delegate = dataSourceDelegate 29 contentsCollectionView.dataSource = dataSourceDelegate 30 31 rowInTableViewCell = row 32 33 contentsCollectionView.reloadData() 34 } 35}

NewMovieCollectionViewCell

1import UIKit 2 3class NewMovieCollectionViewCell: UICollectionViewCell { 4 5 @IBOutlet var contentsImageView: UIImageView! 6 @IBOutlet var contentsTitleLabel: UILabel! 7 var movieID: Int! 8 9 override func awakeFromNib() { 10 super.awakeFromNib() 11 } 12 13 func nowPlayingSetCell(nowPlayingItem:nowPlayingMovieItem) { 14 15 contentsTitleLabel.text = nowPlayingItem.title 16 movieID = nowPlayingItem.id 17 18 if let poster_path: String = nowPlayingItem.poster_path { 19 let url = "https://image.tmdb.org/t/p/w500/" + poster_path 20 if let data = try? Data(contentsOf: URL(string: url)!){ 21 self.contentsImageView.image = UIImage(data: data) 22 } 23 } else { 24 contentsImageView.image = UIImage(named: "NoImage") 25 } 26 } 27以下似たよう名前の関数が三つ同じ構図であります。 28}

NewMovieViewController

1import UIKit 2 3class NewMovieViewController: UIViewController, MoveDetailVCDelegate { 4 5 @IBOutlet var contentsTableView: UITableView! 6 var registerdMovieID:Int! 7 8 private var nowPlayingItems:[nowPlayingMovieItem] = [nowPlayingMovieItem]() 9 private var upcomingItems:[upcomingMovieItem] = [upcomingMovieItem]() 10 private var popularItems:[popularMovieItem] = [popularMovieItem]() 11 private var topRatedItems:[topRatedMovieItem] = [topRatedMovieItem]() 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 16 configureUI() 17 } 18 19 func configureUI() { 20 21 contentsTableView.delegate = self 22 contentsTableView.dataSource = self 23 24 let nib = UINib(nibName: "NewMovieTableViewCell", bundle: .main) 25 contentsTableView.register(nib, forCellReuseIdentifier: "NewMovieTableViewCell") 26 contentsTableView.tableFooterView = UIView() 27 } 28} 29 30extension NewMovieViewController: UITableViewDelegate, UITableViewDataSource { 31 32 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 33 return 4 34 } 35} 36 37extension NewMovieViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 38 39 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 40 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewMovieCollectionViewCell", for: indexPath) as! NewMovieCollectionViewCell 41 42 if let tableViewCell = collectionView.superview?.superview as? NewMovieTableViewCell, 43 let rowInTableViewCell = tableViewCell.rowInTableViewCell { 44 45 if rowInTableViewCell == 0 { 46 cell.nowPlayingSetCell(nowPlayingItem: nowPlayingItems[indexPath.row]) 47 } else if rowInTableViewCell == 1 { 48 cell.upcomingSetCell(upcomingItem: upcomingItems[indexPath.row]) 49 } else if rowInTableViewCell == 2 { 50 cell.popularSetCell(popularItem: popularItems[indexPath.row]) 51 } else if rowInTableViewCell == 3 { 52 cell.topRatedSetCell(topRatedItem: topRatedItems[indexPath.row]) 53 } 54 } 55 return cell 56 } 57 58func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 59 60 let storyboard: UIStoryboard = UIStoryboard(name: "MovieSearchViewController", bundle: nil) 61 let nextVC = storyboard.instantiateViewController(withIdentifier: "RegisterViewController") as! RegisterViewController 62 nextVC.modalPresentationStyle = .fullScreen 63 nextVC.movieID = 64 self.navigationController?.pushViewController(nextVC, animated: true) 65 66 print(indexPath.row) 67 collectionView.deselectItem(at: indexPath, animated: true) 68 } 69}

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

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

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

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

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

hoshi-takanori

2021/01/03 16:49

didSelect でやりたいことって、対応する movieItem の配列から id を取得して nextVC に渡すことですよね。そこまでできてるなら、そんなに難しいとは思えないのですが…。 あと、余計なお世話ですが、Swift の習慣では型名は大文字で始めるのが一般的なのと、rowInTableViewCell が Int なのは美しくないのと、cellForItemAt で collectionView の superview の superview が tableViewCell であることを仮定するのはちょっと怪しいかなってあたりが気になりました。
Ytan

2021/01/04 14:55

ご指摘ありがとうございます。 そうですね、rowInTableViewCellでSectionを分けているのでそれぞれの型を持ったプロパティ[indexPath.row].idとやれば選択されたrowのデータを取得することができました。 CollectionViewはTableViewに属していてTableView各セクションで横スクロールできるCollectionViewを配置しているCollectionView の親の親は TableViewになっております。
guest

回答1

0

自己解決

押されたセルをどのセクションか識別しそのindexPathのid情報をそれぞれ渡しました。

if let tableViewCell = collectionView.superview?.superview as? NewMovieTableViewCell, let rowInTableViewCell = tableViewCell.rowInTableViewCell { if rowInTableViewCell == 0 { nextVC.movieID = nowPlayingItems[indexPath.row].id } else if rowInTableViewCell == 1 { nextVC.movieID = upcomingItems[indexPath.row].id } else if rowInTableViewCell == 2 { nextVC.movieID = popularItems[indexPath.row].id } else if rowInTableViewCell == 3 { nextVC.movieID = topRatedItems[indexPath.row].id } }

投稿2021/01/05 13:21

Ytan

総合スコア39

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問