I have a UITableView
with a UICollectionView
within each of its rows like the illustration below.
source: https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
The intention of my app is to display a character set of a language within each table view row. Each character is contained within a collection view cell of the collectionview within the corresponding row.
The problem I am having is that the english character set is being displayed for every tableview row.
This is because each collectionview only has one section and thus every collectionview uses the same indexPath.section
value of zero.
What I need to do is get the section value of the table view cells that the collectionviews are within and somehow pass that to
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
I've tried several things but I can't find a way to access the tableview section value from the collectionview within it.
My code is a bit of a mess but the generally important parts are
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HorizontalSlideCell", for: indexPath)
return cell
}
...
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InnerCollectionViewCell",
for: indexPath as IndexPath)
//format inner collectionview cells
//indexPath.section is the collectionview section index but needs to be its parent tableview section's index. How do I get it?
cellCharLabel?.text = Languages.sharedInstance.alphabets[indexPath.section].set[indexPath.row].char
cellCharLabel?.textAlignment = .center
cellCharLabel?.font = UIFont(name: "Helvetica", size: 40)
cell.contentView.addSubview(cellCharLabel!)
return cell
}
You can set the collectionView
tag to make it.
CollectionView
should be the subview of tableViewCell
. That is the collectionView
can be the property of your customize TableViewCell
Seems you are using Prototype Cell
.
class YourCustomizeTableViewCell: UITableViewCell {
let collectionView: CollectionView
...
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HorizontalSlideCell", for: indexPath) as! YourCustomizeTableViewCell
cell.collectionView.tag = indexPath.row
return cell
}
...
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InnerCollectionViewCell",
for: indexPath as IndexPath)
//indexPath.section is the collectionview section index but needs to be its parent tableview section's index. How do I get it?
cellCharLabel?.text = Languages.sharedInstance.alphabets[collectionView.tag].set[indexPath.row].char
...
return cell
}
I'm assuming you have a custom UITableViewCell class with an instance of a UICollectionView, so you just need to pass it the section index when you call cellForRowAtIndexPath.
You would need to make a var in the tableViewCell class to hold the section index.
class CustomTableViewCell: UITableViewCell {
var sectionIndex:Int?
}
Then when calling cellForRow... you just pass in the section to that cell.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HorizontalSlideCell", for: indexPath) as CustomTableViewCell
cell.sectionIndex = indexPath.section
return cell
}
Not sure how you are loading data into the collection views as you aren't showing that but once your table view cell has the section you can then do a number of things to load your data.
Let me know if you need more detail
来源:https://stackoverflow.com/questions/40667585/how-to-get-section-of-uitableview-from-inside-a-child-uicollectionview