【发布时间】: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