ios - UICollectionView : can't set value 内的 UITableView

标签 ios swift uitableview uicollectionview

我有一个带有 4 个 UICollectionViewCell 的 UICollectionView。每个单元格都包含一个 UITableView,其中包含不同数量的单元格。

我有一个数组,我需要从中将一个值(取决于 indexPath)传递到单元格中。我的问题是,当我尝试这样做时,它总是给我 nil。

例如我有这个数组:

let arrayOfStrings = ["test1", "test2", "test3", "test4"]

我的 Collection View 的 cellForItemAt 方法中有这段代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "testCollectionViewCell", for: indexPath) as! TestCollectionViewCell

    cell.myString = arrayOfStrings[indexPath.row]

    cell.label.text = "This text is set."
    return cell
}

单元格的标签设置正确,但是当我尝试在单元格的 TableView 的 cellForRowAt 方法中打印 myString 时,它总是返回 nil。 我需要该字符串,因为我计划根据传入的字符串来操作 TableView 。

那么为什么会发生这种情况以及可以做什么?

最佳答案

你可以这样试试

extension HomeViewController : UITableViewDelegate, UITableViewDataSource {
   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return 4
   }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

  if indexPath.row == 0 {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DealTableViewCell", for: indexPath) as! DealTableViewCell
    cell.DealCollectionView.dataSource = self
    cell.DealCollectionView.delegate = self
    cell.DealCollectionView.tag = 10101
    return cell
} else if indexPath.row == 1 {
    let cell = tableView.dequeueReusableCell(withIdentifier: "BusinessTableViewCell", for: indexPath) as! BusinessTableViewCell
    cell.BusinessCollectionView.dataSource = self
    cell.BusinessCollectionView.delegate = self
    cell.BusinessCollectionView.tag = 10102
    return cell
} else if indexPath.row == 2 {
    let cell = tableView.dequeueReusableCell(withIdentifier: "FilterTableViewCell", for: indexPath) as! FilterTableViewCell
    cell.FilterCollectionView.dataSource = self
    cell.FilterCollectionView.delegate = self
    cell.FilterCollectionView.tag = 10103
    return cell
} else {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ComboTableViewCell", for: indexPath) as! ComboTableViewCell
    cell.ComboCollectionView.dataSource = self
    cell.ComboCollectionView.delegate = self
    cell.ComboCollectionView.tag = 10104
    return cell
   }
  }
}

将 Collection View 添加到每个tableview cell 并在tableview cell 中设置delegate

   extension HomeViewController : UICollectionViewDelegate, UICollectionViewDataSource {

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  if collectionView.tag == 10101 {
    return 3
  } else if collectionView.tag == 10102 {
    return 3
  } else if collectionView.tag == 10103 {
    return 3
  } else if collectionView.tag == 10104 {
    return 3
 }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView.tag == 10101 {
    let cell = collectionView.dequeueReusableCell(withIdentifier: "DealCollectionViewCell", for: indexPath) as! DealCollectionViewCell

    return cell
} else if collectionView.tag == 10102 {
    let cell = collectionView.dequeueReusableCell(withIdentifier: "BusinessCollectionViewCell", for: indexPath) as! BusinessCollectionViewCell

    return cell
} else if collectionView.tag == 10103 {
    let cell = collectionView.dequeueReusableCell(withIdentifier: "FilterCollectionViewCell", for: indexPath) as! FilterCollectionViewCell

    return cell
} else if collectionView.tag == 10104 {
    let cell = collectionView.dequeueReusableCell(withIdentifier: "ComboCollectionViewCell", for: indexPath) as! ComboCollectionViewCell

    return cell
}

return UICollectionViewCell()
}
}

重新加载正在使用的collectionView

if let cell = self.tableView.cellForRow(at: IndexPath(row: i, section: 0)) as? 
tableViewCell {
  cell.collectionView.reloadData()
 }

关于ios - UICollectionView : can't set value 内的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59294388/

相关文章:

ios - 使用 iOS 8 灵活的表格单元格,单元格高度改变但文本不换行

ios - Alamofire 5 委托(delegate)中缺少 sessionDidReceiveChallenge

ios - 谷歌地图 iOS SDK : custom icons to be used as markers

swift - 为什么不调用方法 updateSearchResultsForSearchController?

json - 从 json 字符串构建 url 时,Alamofire 返回无效的 url

ios - 出现删除按钮时重新计算自定义 UITableViewCell 的高度

ios - 创建 Apple Music 开发者 token

ios - 用 SKShapeNode 画一条线

ios - 为什么 shouldStartLoadWithRequest 不在 webview 中加载 my/amp 版本,而在 safari 中加载?

ios - 在容器 View 中快速访问和更新 TableView