将数据从集合视图单元格内的表格视图单元格传输到另一个集合视图单元格[带图片!]

时间:2017-07-13 14:08:53

标签: ios swift uitableview delegates uicollectionview

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

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

  1. 集合视图单元格应将索引更改为当前+1
  2. 应将表格视图单元格数据(在本例中为标题和日期)传递到新的集合视图单元格标题属性。
  3. 另一个方面是表视图存储在容器视图类中。我不确定这是否有关系,但它是一个额外的层来传递变量。

    到目前为止,这是我遇到的问题

    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?
    

    Symbolic setup for my problem

2 个答案:

答案 0 :(得分: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
  }
}

希望有所帮助!

答案 1 :(得分:-1)