UICollectionViewCell 内带有 UITableView 的自定义单元格 [英] Custom cell with UITableView inside UICollectionViewCell

查看:29
本文介绍了UICollectionViewCell 内带有 UITableView 的自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于 UICollectionView 的自定义 UICollectionViewCell.我已将此自定义类标记为 UITableViewDataSource 和 UITableViewDelegate,以便将 UITableView 放在 Collection 视图的每个单元格中.

I have a custom UICollectionViewCell for a UICollectionView. I have marked this custom class as UITableViewDataSource and UITableViewDelegate in order to put UITableView in each cell of Collection view.

表视图在集合视图的每个单元格中正确呈现,但问题在于这些表视图标题和单元格中的数据.

The table view is rendering properly in each cell of collection view but the issue is with the data in those table view header and cells.

示例:我有 10 个问题,每个问题有 5 到 6 个选项.我将集合视图中的项目数保留为 questionList 的计数,这会创建正确数量的集合视图单元格

Example: I have 10 questions with 5 to 6 options for each question. I have kept number of items in collection view as count of questionList which creates correct number of collection view cells

 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return questionList.count
    }

当我创建每个单元格时:

When i create each cell as below:

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! QuestionCell

        return cell
    }

自定义单元格 (QuestionCell) 呈现正确,但所有自定义集合视图单元格仅显示带有选项的第一个问题.我的问题是如何将每个集合视图单元格的 indexPath.item 与 QuestionCell 类中的 TableView 单元格的 indexPath 链接起来.

The custom cell (QuestionCell) is rendering properly but all the custom collection view cells are displaying only the first question with it's options. Question here i have is that how can i link the indexPath.item of each collection view cell with the indexPath of TableView cell inside QuestionCell class.

fileprivate func setUpViews(){
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(QuestionHeader.self, forHeaderFooterViewReuseIdentifier: headerId)
        tableView.register(OptionCell.self, forCellReuseIdentifier: cellId)
    }

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerId) as! QuestionHeader

    header.nameLabel.text = questionList[section].questionText

    return header
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


    return questionList[section].options.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! OptionCell

    cell.nameLabel.text = questionList[indexPath.row].options[indexPath.row].optionText

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50.0
}

任何关于此的想法将不胜感激.

Any idea to proceed on this will be highly appreciated.

推荐答案

我能够通过在单独的 Swift 类中定义 IndexPath 变量并在 CollectionView 的 cellForItem 函数中设置它的值来解决该问题.通过这种方法,我能够在相应的 TableViewCell 中引用每个 CollectionView 单元格的 indexPath.

I was able to resolve the issue by defining an IndexPath variable in a separate Swift class and setting it's value inside CollectionView's cellForItem function. With this approach, I was able to refer indexPath of each CollectionView cell inside the corresponding TableViewCell.

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

  cellIndex = indexPath.item

    print("Cell index: ",cellIndex)
    if indexPath.item > 0{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! PageCell
        cell.tableView.reloadData()
        return cell
    }
    else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! PageCell
        cell.tableView.reloadData()
        return cell
    }

}

这篇关于UICollectionViewCell 内带有 UITableView 的自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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