2017-07-13 73 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

謝謝! 多麼美妙的清晰,結構合理的答案。 如果這是火種,我願意給你一個超級喜歡;) –