【问题标题】:Transfer data from table view cell inside a collection view cell, to another collection view cell [with picture!]将数据从集合视图单元格内的表视图单元格传输到另一个集合视图单元格 [带图片!]
【发布时间】:2017-07-13 14:08:52
【问题描述】:

我正在开发一个应用程序,我在集合视图单元格和其中包含的表格视图之间陷入困境。

我的第一个集合视图单元格包含一个带有表格视图单元格的表格视图。 每个表格视图单元格都包含保存的数据,选择一个单元格会发生两件事。

  1. 集合视图单元格应将索引更改为当前 +1
  2. 应将表格视图单元格数据(在本例中为标题和日期)传递到新的集合视图单元格标题属性。

另一方面是表视图存储在容器视图类中。我不确定这是否重要,但它是传递变量的额外层。

到目前为止,这是我卡住的地方

tableViewDidselectCode

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let header = LoadHeaderView()
    let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as SavedTableViewCell
    header.titleString = cell.taskLabel.text!
    header.descriptionString = cell.clientLabel.text!
}

如何将其传递给

self -> ContainerView -> collectionCell[0] -> CollectionView -> collectionCell[1] -> tableView -> header?

【问题讨论】:

    标签: ios swift uitableview delegates uicollectionview


    【解决方案1】:

    您的表格视图依赖于父集合视图单元格。这意味着您需要在实例化时将集合视图单元格的引用传递给表视图。我会制定一个协议。

    protocol myTableViewCellDelegate {
      func updateMyCollectionViewCell
    }
    
    extension myCollectionViewCell: myTableViewCellDelegate {
      func updateMyCollectionViewCell {
        // update whatever you need to here
      }
    }
    
    extension myCollectionTableViewDelegate: UITableViewDelegate {
      // ...
      func collectionView(_ collectionView: UICollectionView, 
                          willDisplay cell: UICollectionViewCell, 
                          forItemAt indexPath: IndexPath) {
        // instantiate table view
        // pass self to tableview *as weak reference*
        myTableView.customDelegate = self
      }
      //...
    }
    
    extension myTableViewDelegate: UITableViewDelegate {
      //...
      func tableView(_ tableView: UITableView, 
                     didSelectRowAt indexPath: IndexPath) {
        // instantiate table view cell
        // assuming you're not using custom cell class
        tableCell.updateMyCollectionViewCell()
    
        // if you are using custom cell class then the tableViewDelegate
        // will need to pass collectionView reference on to the cells
      }
    }
    

    希望有帮助!

    【讨论】:

    • 谢谢!多么美妙的清晰和结构良好的答案。如果这是 Tinder,我会给你一个超级喜欢 ;)
    【解决方案2】: