2017-07-13 77 views
1

我上的应用,在那里我被困在集合视图中的细胞以及其中所包含的表视图之间的工作无人过问。传输数据[带图像!]

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

  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? 

Symbolic setup for my problem

回答

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

谢谢! 多么美妙的清晰,结构合理的答案。 如果这是火种,我愿意给你一个超级喜欢;) –