2

我的每一行都有一个,如下图所示UITableViewUICollectionView

表视图中的集合视图

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

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

我的应用

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

这是因为每个集合视图只有一个部分,因此每个集合视图都使用相同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: "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
}
4

2 回答 2

7

您可以设置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
}
于 2016-11-18T03:59:04.200 回答
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
}

不确定如何将数据加载到集合视图中,因为您没有显示它,但是一旦您的表格视图单元格具有该部分,您就可以做很多事情来加载您的数据。

如果您需要更多详细信息,请告诉我

于 2016-11-18T01:00:04.627 回答