2016-11-18 62 views
0

我有一個UITableViewUICollectionView其中每行如下圖所示。如何從兒童內部獲取UITableView的部分UICollectionview

collectionviews within tableviews

源:https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

我的應用程序的目的是爲了顯示一個字符集的每個表視圖行內的語言。每個字符都包含在相應行內collectionview的集合視圖單元中。

my app

我遇到的問題是英文字符集正在顯示,每行的tableview。

這是因爲每個collectionview只有一個部分,因此每個collectionview使用相同的indexPath.section值爲零。

我需要做的就是表視圖細胞的collectionviews是內並以某種方式傳遞到

func collectionView(_ collectionView: UICollectionView, 
        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 

我已經試過幾件事情的區間值,但我不能找到一種方法從其中的collectionview訪問tableview部分的值。

我的代碼是一個有點亂,但一般重要的部分是

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "HorizontalSlideCell", for: indexPath) 

    return cell 
} 

... 

func collectionView(_ collectionView: UICollectionView, 
        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InnerCollectionViewCell", 
                for: indexPath as IndexPath) 

    //format inner collectionview cells 

    //indexPath.section is the collectionview section index but needs to be its parent tableview section's index. How do I get it? 
    cellCharLabel?.text = Languages.sharedInstance.alphabets[indexPath.section].set[indexPath.row].char 
    cellCharLabel?.textAlignment = .center 
    cellCharLabel?.font = UIFont(name: "Helvetica", size: 40) 

    cell.contentView.addSubview(cellCharLabel!) 

    return cell 
} 

回答

2

您可以設置collectionView標籤做出來。 CollectionView應該是tableViewCell的子視圖。那就是collectionView可以是你定製的屬性TableViewCell似乎你使用的是Prototype Cell

class YourCustomizeTableViewCell: UITableViewCell { 
    let collectionView: CollectionView 
    ... 
} 



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "HorizontalSlideCell", for: indexPath) as! YourCustomizeTableViewCell 
    cell.collectionView.tag = indexPath.row 

    return cell 
} 

... 

func collectionView(_ collectionView: UICollectionView, 
        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InnerCollectionViewCell", 
               for: indexPath as IndexPath) 


//indexPath.section is the collectionview section index but needs to be its parent tableview section's index. How do I get it? 
cellCharLabel?.text = Languages.sharedInstance.alphabets[collectionView.tag].set[indexPath.row].char 
... 
return cell 
} 
+0

乾杯。正是我在找的! – Danoram

+0

如果我們在集合視圖中傳遞標籤,但由於可重用的表視圖單元格,我正在獲取上一個單元格和以前的標籤。 – user1960279

+0

@ user1960279你在'''cell.collectionView.tag = indexPath.row'後面看到collectionView的標籤被重新分配了。你在哪裏使用標籤? –

2

我假設你有一個UICollectionView實例的自定義的UITableViewCell類,所以你只需要通過它的部分索引當您調用cellForRowAtIndexPath。

您需要在tableViewCell類中創建一個var來存放節索引。

class CustomTableViewCell: UITableViewCell { 
     var sectionIndex:Int? 

} 

然後,當調用cellForRow ...時,您只需將該部分傳遞給該單元格。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "HorizontalSlideCell", for: indexPath) as CustomTableViewCell 
    cell.sectionIndex = indexPath.section 
    return cell 
} 

不知道你是如何將數據加載到收集意見,你不顯示,但一旦你的表視圖細胞有部分你就可以做很多事情來加載數據。

讓我知道如果您需要更詳細

+0

這是有幫助的。謝謝! – Danoram

+1

對不起,我還沒有得到如何從CollectionViewCell訪問sectionIndex,你能提供一個例子嗎?謝謝(非常急) – Gulz