Collection-View-in-a-Table-View-Cell
Collection-View-in-a-Table-View-Cell copied to clipboard
Does not handle tableview with multiple sections
This code relies on the tag property. Because of this, it does not account for tableviews with multiple sections. How would you modify the code to account for this? Could you create a custom UICollectionView with custom properties "section" and "row", and store the tableview section and row in those properties? Would that violate MVC? Thanks!
Great question! I did this once before, basically I just subclassed UICollectionView
to add a property to store the index (before I used tag). You could use the same technique to store an index path instead, or use ObjC's associated objects, but I wouldn't recommend it :wink:
Does that make sense?
Hi guys,
I could make the UITableView
sections work without subclassing the UICollectionView
nor adding new properties. Just used indexPath.section
instead of indexPath.row
in the tableView willDisplayCell:
method:
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.section)
PS: Of course you will also need to add the UITableView
delegate methods to indicate the number of sections, title, etc.
Hope it helps, any doubts just let me know.
i tried to get different Data for uicollectionView but it didn't work.
in TableView tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.section)
in CollectionView if (indexPath.section == 0) { cell.titleItem.text = self.songName[indexPath.row] }
if (indexPath.section == 1) {
cell.titleItem.text = self.songUrl[indexPath.row]
}
it showed same text every section in tableView
In the collectionView
you should have something like this:
cell.titleItem.text = self.songURL[collectionView.tag][indexPath.item]
I don't think you need to check for the indexPath.section == 1
there.
Hope it works for you.
Hi guys,
I could make the
UITableView
sections work without subclassing theUICollectionView
nor adding new properties. Just usedindexPath.section
instead ofindexPath.row
in thetableView willDisplayCell:
method:tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.section)
PS: Of course you will also need to add the
UITableView
delegate methods to indicate the number of sections, title, etc.Hope it helps, any doubts just let me know.
How did you used that table section then inside the collectionView method?