【问题标题】:Custom cell with UITableView inside UICollectionViewCellUICollectionViewCell 内带有 UITableView 的自定义单元格
【发布时间】:2018-08-27 19:14:41
【问题描述】:

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

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

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

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

当我如下创建每个单元格时:

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 链接起来。

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
}

我们将不胜感激。

【问题讨论】:

    标签: swift uitableview uicollectionviewcell


    【解决方案1】:

    我认为 questionList[section].options.count 必须改变。

    您想在每个集合视图单元格中放置一个问题列表吗?

    比使用collectionView的indexPath作为questionList的索引而不是tableView的部分

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return questionList[section].options.count
    }
    

    【讨论】:

    • 谢谢桑。我已经发布了我对这个问题的解决方案。我昨天能够解决这个问题。
    【解决方案2】:

    是否应该只将 questionList 中的一个问题而不是整个列表传递给您的 QuestionCell? 在您的 QuestionCell 中,它应该如下所示:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerId) as! QuestionHeader
    
        header.nameLabel.text = question.questionText
    
        return header
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    
        return question.options.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! OptionCell
    
        cell.nameLabel.text = question.options[indexPath.row].optionText
    
        return cell
    }
    

    【讨论】:

    • 谢谢。我能够解决这个问题。请看我今天发布的答案。
    【解决方案3】:

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

    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
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多