问题
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.
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
}
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.
回答1:
i think questionList[section].options.count has to change.
you want a put question list in each collection view cell right?
than use collectionView's indexPath as index of questionList not tableView's section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questionList[section].options.count
}
回答2:
Should only one question from the questionList be passed to your QuestionCell rather than the entire list? In your QuestionCell, it should look like:
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:
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
}
}
来源:https://stackoverflow.com/questions/52045681/custom-cell-with-uitableview-inside-uicollectionviewcell