2017-09-14 63 views
1

所以我有3個單元格的集合視圖,其中每個單元格都有一個tableview。很明顯,每個集合視圖單元必須使用表視圖單元的不同數據加載它的表視圖。如何在UicollectionViewCell中顯示UItableView?

我的第一個想法是讓集合查看錶視圖的委託和datasrouce,但即使如此,數據源也需要知道正在加載哪個集合視圖單元格。

還以爲莊家對DS和委託兩個集合視圖和表視圖一個單獨的類的,不過話又說回來,我卡上實現代碼如下DS怎麼會知道哪個集合觀察室它正在從加載。

有什麼想法?

編輯:

我做任務後,數據源字段是零。那麼我在這裏錯過了什麼?

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) else { 
     return UICollectionViewCell.init() 
    } 

    cell.backgroundColor = UIColor.clear 

    cell.theTableView.dataSource = RequestedTableDataSource.init() 
    cell.theTableView.delegate = self 

    cell.theTableView.reloadData() 

    return cell 
} 

class RequestedTableDataSource: NSObject, UITableViewDataSource { 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 16 
} 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "RequestedCell") as? IntervieologistRequestedCell else { 
     return UITableViewCell.init() 
    } 

    cell.setupCellFor(name: "Dan", image: UIImage.init(named: "dan")!) 

    return cell 
} 

}

如果我這樣做:

let foo = RequestedTableDataSource.init() 
    cell.myTableView.dataSource = foo 

那麼數據源字段設置,numberOfRows被調用,但的cellForRowAtIndexPath沒有。

+0

讓您的CollectionView細胞作爲數據源爲您的TableView,並在通話的CollectionView的reloadTableview ... cellForItemAt我希望它的幫助,請發表評論,如果您需要更多幫助 –

+1

找到它。我需要對RequestedTableDataSource對象的強引用來切換到表的dataSource字段。 – mickm

回答

0

您可以將tableView.tag設置爲一個唯一的整數,稍後在使用該方法的委託方法中標識它,但我建議爲每個tableview保留一個單獨的數據源/委託,更清潔。

請勿將CollectionView或任何其他View對象作爲數據源/委託。

如設置

collectionViewCell.tableView.tag = index; 
collectionViewCell.tableView.delegate = commonDelegate; 

collectionViewCell.tableView.delegate = uniqueDelegate; // Better choice