如何从子UICollectionview中获取UITableView的部分

Dan*_*ram 2 uitableview uicollectionview swift swift3 indexpath

我在每行中都有一个UITableViewUICollectionView如下图所示。

tableviews中的collectionviews

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

我的应用程序的目的是在每个表格视图行中显示一种语言的字符集。每个字符都包含在相应行内collectionview的collection view单元格内。

我的应用

我遇到的问题是正在为每个tableview行显示英文字符集。

这是因为每个collectionview仅具有一个部分,因此每个collectionview使用相同indexPath.section的零值。

我需要做的是获取collectionview所在的表视图单元格的section值,并以某种方式将其传递给

func collectionView(_ collectionView: UICollectionView,
                    cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
Run Code Online (Sandbox Code Playgroud)

我已经尝试了几件事,但是找不到从其中的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
}
Run Code Online (Sandbox Code Playgroud)

Wil*_* Hu 6

您可以设置collectionView标签来制作它。 CollectionView应该是的子视图tableViewCell。那collectionView可以是TableViewCell 您正在使用的自定义Seem 的属性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
}
Run Code Online (Sandbox Code Playgroud)