How to get section of UITableView from inside a child UICollectionview

拜拜、爱过 提交于 2019-12-22 18:20:12

问题


I have a UITableView with a UICollectionView within each of its rows like the illustration below.

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

The intention of my app is to display a character set of a language within each table view row. Each character is contained within a collection view cell of the collectionview within the corresponding row.

The problem I am having is that the english character set is being displayed for every tableview row.

This is because each collectionview only has one section and thus every collectionview uses the same indexPath.section value of zero.

What I need to do is get the section value of the table view cells that the collectionviews are within and somehow pass that to

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

I've tried several things but I can't find a way to access the tableview section value from the collectionview within it.

My code is a bit of a mess but the generally important parts are

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
}

回答1:


You can set the collectionView tag to make it. CollectionView should be the subview of tableViewCell. That is the collectionView can be the property of your customize TableViewCell Seems you are using 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
}



回答2:


I'm assuming you have a custom UITableViewCell class with an instance of a UICollectionView, so you just need to pass it the section index when you call cellForRowAtIndexPath.

You would need to make a var in the tableViewCell class to hold the section index.

 class CustomTableViewCell: UITableViewCell {
      var sectionIndex:Int?

 }

Then when calling cellForRow... you just pass in the section to that cell.

 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
}

Not sure how you are loading data into the collection views as you aren't showing that but once your table view cell has the section you can then do a number of things to load your data.

Let me know if you need more detail



来源:https://stackoverflow.com/questions/40667585/how-to-get-section-of-uitableview-from-inside-a-child-uicollectionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!