Collection-View-in-a-Table-View-Cell icon indicating copy to clipboard operation
Collection-View-in-a-Table-View-Cell copied to clipboard

Does not handle tableview with multiple sections

Open lnjustin opened this issue 9 years ago • 5 comments

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!

lnjustin avatar Dec 01 '15 10:12 lnjustin

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?

ashfurrow avatar Dec 01 '15 17:12 ashfurrow

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.

PPacie avatar Dec 12 '15 22:12 PPacie

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

dinhcuong1989 avatar Dec 18 '15 13:12 dinhcuong1989

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.

PPacie avatar Mar 25 '16 17:03 PPacie

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.

How did you used that table section then inside the collectionView method?

mujtuba11khan avatar Mar 06 '20 11:03 mujtuba11khan