【问题标题】:SWIFT: Communicating data from ViewController where tableView is defined to tableViewCellSWIFT:从定义 tableView 到 tableViewCell 的 ViewController 通信数据
【发布时间】:2020-10-05 14:41:47
【问题描述】:

我已经在 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. MoviesViewController5 中填充moviesItemsArray
  2. 根据索引将moviesItemsArray中的每个moviesItems与每个对应的TableViewCell3通信
  3. 将接收到的电影数据影响到TableViewCell3的类属性moviesItems
  4. collectionViewTest的帮助下显示TableViewCell3中的电影数据

问题(第 2 步):我不知道如何根据索引将 moviesItemsArray 中的每个 moviesItems 传达给每个对应的 TableViewCell3
注意: 其他步骤已经完成。


问题:我是否应该像在 SWIFT 中的不同类之间进行任何通信一样通信数据,或者需要在 TableViewCell3 中使用 collectionViewTest.dataSource 完成某些事情

【问题讨论】:

  • 请一次只问一个问题。从简单开始... 如果您解决问题以便只问第一个问题,您可能会自己找到答案。至少,您可以发布一个独立的问题。
  • 问题是如何将信息传达给 tableViewCell。我只是想提供两种我想过的方法(这是不必要的,我可以问一个问题:我该怎么做)

标签: ios swift uitableview tvos uitableviewdiffabledatasource


【解决方案1】:

问题是如何将信息传达给 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
}

【讨论】:

    【解决方案2】:

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

      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 是用于通信的属性:

    class CatalogueTableViewCell: UITableViewCell {
    
      internal var images: [UIImage]! 
        } 
    

    【讨论】:

    • 和我的回答一样。
    • @matt 您没有提供详细信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多