gpt4 book ai didi

swift - UICollectionViewCell 中带有 UITableView 的自定义单元格

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:51 24 4
gpt4 key购买 nike

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

表格 View 在 Collection View 的每个单元格中正确呈现,但问题在于这些表格 View 标题和单元格中的数据。

示例:我有 10 个问题,每个问题有 5 到 6 个选项。我在 Collection View 中保留了项目数作为 questionList 的计数,它创建了正确数量的 Collection View 单元格

 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) 正在正确呈现,但所有自定义 Collection View 单元格仅显示第一个问题及其选项。我的问题是如何将每个 Collection View 单元格的 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
}

任何继续进行此操作的想法都将受到高度赞赏。

最佳答案

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

您想要在每个 Collection View 单元格中放置一个问题列表吗?

比使用 collectionView 的 indexPath 作为问题列表的索引而不是 tableView 的部分

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

关于swift - UICollectionViewCell 中带有 UITableView 的自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52045681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com