Home>
Current status

In TableView, TableView has CollectionView in 4 sections, each with 4 APIs.
The acquired Image and Label are displayed by horizontal scrolling.

APIStructEach structure of has an ID (let id: Int).

I want to achieve

API ID data of Cell that was didSelected in CollectionView
I want to get it with ViewController and pass the value to the transition destination View along with the transition.
The API display is a little complicated, so please help me.

In code
With NewMovieViewControllerdidSelectSometimes registered in the cell of NewMovieCollectionViewCell placed in NewMovieTableViewCelllet id: IntAssign the value of to registeredMovieID in NewMovieViewControllerdidSelectItemAtI want to put a value on the property of the transition destination of nextVC

It seems a little complicated because it is the value of each cell of CollectionView in 4Section in CustomCell.
I can create a structure-compliant property in NewMovieViewController and access id, title, poster_path, but since there is only one section, I know how to handle the pressed custom cell value in ViewController. I want to

Source code
import Foundation
struct nowPlayingMovieStruct: Codable {
    let results: [nowPlayingMovieItem]
}
struct nowPlayingMovieItem: Codable {
    let id: Int
    let title: String
    let poster_path: String?
}
Below, three Structs with similar names have the same composition.
import UIKit
class NewMovieCollectionViewCell: UITableViewCell {
    @IBOutlet var contentsCollectionView: UICollectionView!
    @IBOutlet var titleLabel: UILabel!
    var rowInTableViewCell: Int?
    override func awakeFromNib () {
        super.awakeFromNib ()
        // Initialization code
        let nib = UINib (nibName: "NewMovieCollectionViewCell", bundle: .main)
        contentsCollectionView.register (nib, forCellWithReuseIdentifier: "NewMovieCollectionViewCell")
    }
    override func setSelected (_ selected: Bool, animated: Bool) {
        super.setSelected (selected, animated: animated)
        // Configure the view for the selected state
    }
    func setCollectionViewDataSourceDelegate
      <D: UICollectionViewDataSource&UICollectionViewDelegate>      (dataSourceDelegate: D, forRow row: Int) {contentsCollectionView.delegate = dataSourceDelegate
        contentsCollectionView.dataSource = dataSourceDelegate
        rowInTableViewCell = row
        contentsCollectionView.reloadData ()
    }
}
import UIKit
class NewMovieCollectionViewCell: UICollectionViewCell {
    @IBOutlet var contentsImageView: UIImageView!
    @IBOutlet var contentsTitleLabel: UILabel!
    var movieID: Int!
    override func awakeFromNib () {
        super.awakeFromNib ()
    }
    func nowPlayingSetCell (nowPlayingItem: nowPlayingMovieItem) {
        contentsTitleLabel.text = nowPlayingItem.title
        movieID = nowPlayingItem.id
        if let poster_path: String = nowPlayingItem.poster_path {
            let url = "https://image.tmdb.org/t/p/w500/" + poster_path
            if let data = try? Data (contentsOf: URL (string: url)!) {
                self.contentsImageView.image = UIImage (data: data)
            }
        } else {
            contentsImageView.image = UIImage (named: "NoImage")
        }
    }
Below, three functions with similar names have the same composition.
}
import UIKit
class NewMovieViewController: UIViewController, MoveDetailVCDelegate {
    @IBOutlet var contentsTableView: UITableView!
    var registerdMovieID: Int!
    private var nowPlayingItems: [nowPlayingMovieItem] = [nowPlayingMovieItem] ()
    private var upcomingItems: [upcomingMovieItem] = [upcomingMovieItem] ()
    private var popularItems: [popularMovieItem] = [popularMovieItem] ()
    private var topRatedItems: [topRatedMovieItem] = [topRatedMovieItem] ()
    override func viewDidLoad () {
        super.viewDidLoad ()
        configureUI ()
    }
    func configureUI () {contentsTableView.delegate = self
        contentsTableView.dataSource = self
        let nib = UINib (nibName: "NewMovieTableViewCell", bundle: .main)
        contentsTableView.register (nib, forCellReuseIdentifier: "NewMovieTableViewCell")
        contentsTableView.tableFooterView = UIView ()
    }
}
extension NewMovieViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int)->Int {
        return 4
    }
}
extension NewMovieViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView (_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)->UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell (withReuseIdentifier: "NewMovieCollectionViewCell", for: indexPath) as! NewMovieCollectionViewCell
        if let tableViewCell = collectionView.superview? .superview as? NewMovieTableViewCell,
            let rowInTableViewCell = tableViewCell.rowInTableViewCell {
            if rowInTableViewCell == 0 {
                cell.nowPlayingSetCell (nowPlayingItem: nowPlayingItems [indexPath.row])
            } else if rowInTableViewCell == 1 {
                cell.upcomingSetCell (upcomingItem: upcomingItems [indexPath.row])
            } else if rowInTableViewCell == 2 {
                cell.popularSetCell (popularItem: popularItems [indexPath.row])
            } else if rowInTableViewCell == 3 {
                cell.topRatedSetCell (topRatedItem: topRatedItems [indexPath.row])
            }
        }
        return cell
    }
func collectionView (_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let storyboard: UIStoryboard = UIStoryboard (name: "MovieSearchViewController", bundle: nil)
        let nextVC = storyboard.instantiateViewController (withIdentifier: "RegisterViewController") as! RegisterViewController
        nextVC.modalPresentationStyle = .fullScreen
        nextVC.movieID =
        self.navigationController? .PushViewController (nextVC, animated: true)
        print (indexPath.row)
        collectionView.deselectItem (at: indexPath, animated: true)
    }
}
  • Answer # 1

    We identified which section the pressed cell was and passed the id information for its indexPath.

    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
                }
            }