Custom cell with UITableView inside UICollectionViewCell
Custom cell with UITableView inside UICollectionViewCell
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.
3 Answers
3
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
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
Thanks. I was able to resolve the issue. Please see the answer I posted today.
– Pratul
Aug 29 at 13:54
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
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks San. I have posted my solution for this question. I was able to resolve this issue yesterday.
– Pratul
Aug 29 at 13:52