SWIFT:从ViewController传递数据,其中tableView定义为tableViewCell

问题描述

我已经在viewController中以编程方式实现了tableView:

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

tableViewCell也以编程方式实现:

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

我要执行的操作:

  1. moviesItemsArray内填充MoviesViewController5
  2. 根据索引将moviesItems中的每个moviesItemsArray与每个通讯人TableViewCell3进行通信
  3. 影响接收到的电影数据到moviesItems的class属性TableViewCell3
  4. 借助TableViewCell3collectionViewTest显示电影数据

问题(第2步):我不知道如何根据索引将moviesItems中的每个moviesItemsArray与每个通讯人TableViewCell3进行通信。 注意: 其他步骤已经完成。


问题:我应该像在SWIFT中不同类之间进行通信一样传递数据,还是需要在collectionViewTest.dataSource内使用TableViewCell3进行某些操作

解决方法

问题是我该如何将信息传达给tableViewCell

这就是cellForRowAt的作用。您的实现目前实际上是空的:

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

刚刚在一个类似的项目中找到了答案:

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

图片是用于交流的属性:

class CatalogueTableViewCell: UITableViewCell {

  internal var images: [UIImage]! 
    }