SWIFT: Communicating data from ViewController where tableView is defined to tableViewCell

145 views Asked by At

I have programmatically implemented a tableView inside a viewController:

class MoviesViewController5: UIViewController {
    let tableView = UITableView()
    
    
    // There's a code responsible for populating this array
    var moviesItemsArray = [[movieItem]]()
    var sectionsData:[[String:Any]]?

    let cellIdentifier = "movieCardCell"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
         tableView.register(sectionTableCell2.self, forCellReuseIdentifier: "tableViewCell")
        displayTableView2()
    }

    func displayTableView2() {
        self.view.addSubview(tableView)
        tableView.translatesAutoresizingMaskIntoConstraints = false

        tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    }
}

extension MoviesViewController5: UITableViewDelegate, UITableViewDataSource {
     func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
      return false
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell =
            tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as? sectionTableCell2
          else {
                fatalError("Unable to create explore table view cell")}
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140
    }
}

The tableViewCell is also implemented programmatically:

class TableViewCell3: UITableViewCell {
    var moviesItems: [movieItem] = []
    let cellIdentifier = "movieCardCell"
    var collectionViewTest:UICollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
    fileprivate let cellOffset: CGFloat = 50
    
    func setupCollectionView() {
        collectionViewTest.delegate = self
        // TODO: Should I communicate moviesItems to TableViewCell3 or set the dataSource to MoviesViewController 5
        collectionViewTest.dataSource = self
    }
}

// Trying to display only one collectionView in tableCell
extension TableViewCell3: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return moviesItems.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
            cellIdentifier, for: indexPath) as! movieCardCell
        cell.movieImageView.sd_setImage(with: URL(string: moviesItems[indexPath.item].imageURL))
        return cell
    }
}

WHAT I WANT TO IMPLEMENT:

  1. Populate moviesItemsArray inside MoviesViewController5
  2. Communicate each moviesItems of moviesItemsArray to each correspondant TableViewCell3 based on the index
  3. Affect the received movie data to the class property moviesItems of TableViewCell3
  4. Display the movies data inside the TableViewCell3 with the help of collectionViewTest

PROBLEM (STEP 2): I don't know how to communicate each moviesItems of moviesItemsArray to each correspondant TableViewCell3 based on the index.
NOTE: The other steps have already been taken care of.


QUESTION: Should I communicate the data like any communication between different classes in SWIFT OR there's something that needs to be done with collectionViewTest.dataSource inside the TableViewCell3

2

There are 2 answers

0
matt On

The question is how can I communicate the information to tableViewCell

That is what cellForRowAt is for. Your implementation is currently effectively empty:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as? sectionTableCell2
    else {
        fatalError("Unable to create explore table view cell")
    }
    // HERE, THIS IS WHERE YOU DO IT
    return cell
}
2
AG_HIHI On

Just found an answer in a similar project:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let _cell = tableView.dequeueReusableCell(withIdentifier: tableCellIdentifier, for: indexPath) as? CatalogueTableViewCell {
      // THIS
      _cell.images = images[indexPath.section]
      return _cell
    } else {
      return UITableViewCell()
    }
  }

Images is the property that's used for communicating:

class CatalogueTableViewCell: UITableViewCell {

  internal var images: [UIImage]! 
    }