如何从子 UICollectionview 中获取 UITableView 的一部分 [英] How to get section of UITableView from inside a child UICollectionview

查看:26
本文介绍了如何从子 UICollectionview 中获取 UITableView 的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView,在它的每一行中都有一个 UICollectionView,如下图所示.

来源:

我遇到的问题是每个 tableview 行都显示英文字符集.

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

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

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

我已经尝试了几件事,但我找不到从里面的 collectionview 访问 tableview 部分值的方法.

我的代码有点乱,但通常重要的部分是

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: "Horizo​​ntalSlideCell", for: indexPath)返回单元格}...func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) ->UICollectionViewCell {let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InnerCollectionViewCell",for: indexPath 作为 IndexPath)//格式化内部collectionview单元格//indexPath.section 是 collectionview 节索引,但需要是其父 tableview 节的索引.我如何得到它?cellCharLabel?.text = Languages.sharedInstance.alphabets[indexPath.section].set[indexPath.row].charcellCharLabel?.textAlignment = .centercellCharLabel?.font = UIFont(name: "Helvetica", size: 40)cell.contentView.addSubview(cellCharLabel!)返回单元格}

解决方案

您可以设置 collectionView 标签来实现.CollectionView 应该是 tableViewCell 的子视图.那就是 collectionView 可以是您自定义的属性 TableViewCell 似乎您正在使用 Prototype Cell.

class YourCustomizeTableViewCell: UITableViewCell {让集合视图:集合视图...}覆盖 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: "Horizo​​ntalSlideCell", for: indexPath) as!YourCustomizeTableViewCellcell.collectionView.tag = indexPath.row返回单元格}...func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) ->UICollectionViewCell {let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InnerCollectionViewCell",for: indexPath 作为 IndexPath)//indexPath.section 是 collectionview 节索引,但需要是其父 tableview 节的索引.我如何得到它?cellCharLabel?.text = Languages.sharedInstance.alphabets[collectionView.tag].set[indexPath.row].char...返回单元格}

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
}

解决方案

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
}

这篇关于如何从子 UICollectionview 中获取 UITableView 的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆