29

Is it possible to use UICollectionView to build a layout where each section can be independently scrolled? For example, imagine 20 rows of images, where each row could be scrolled independently horizontally to reveal more images offscreen (without scrolling other rows in the process); and the entire view could be scrolled vertically to reveal more rows.

I believe something like this could be implemented with several instances of UICollectionView inside a UIScrollView; however, it'd be great to leverage UICollectionView for inserting/moving sections.

I suspect this isn't practical since UICollectionView is a subclass of UIScrollView; but perhaps this can be done with a custom UICollectionViewLayout?

MikeV
  • 1,220
  • 2
  • 13
  • 17
  • You could just use custom cells to get the same effect (instead of sections). Your collection view can act like a table view (single column) and each cell can have a scroll view with a row of images. The nested scroll views should in theory behave nicely since they're fixed to scroll in different directions. I know this doesn't exactly answer the question but can be an alternative solution to get the same general effect. – nebs Jan 08 '13 at 23:20
  • Thanks for the suggestion Nebs - yea, I'll probably go that route if there isn't a way to do it using sections with 1 image per cell. – MikeV Jan 09 '13 at 04:15
  • This question can't be answered right now, since the answer would be a short and disappointing "no". Would you consider rewriting your question so it can be answered, possibly supplying the answer yourself and check the answer? In my opinion, @TheBasicMind deserves the check mark. – epologee Apr 02 '13 at 11:01

2 Answers2

10

You can put multiple UICollectionViews in table cells. The table scrolls vertically. The collection views can be configured to scroll horizontally. I'm doing this on a complex layout and it works well. One constraint to consider is it is much more difficult to do animations that need to move from one table cell to another and you can't use a neat single change of collection view layout to animate all the items in your table view. But if these constraints aren't a problem then this is a relatively easy solution.

TheBasicMind
  • 3,585
  • 1
  • 18
  • 20
  • 3
    Thanks for the suggestion - I actually wound up putting multiple UICollectionViews inside a UICollection view; with the ones inside set up to scroll vertically and the outer one to scroll horizontally. This worked OK, although it would have been simpler if I could have used a single UICollectionView with independent scrolling sections... – MikeV Jan 11 '13 at 21:44
  • 1
    I have done the same but my tableview doesn't scroll smoothly due to this. kindly help me to solve the problem. – HarshIT Feb 07 '14 at 10:28
  • 2
    @HarshIT, it's late I know, but here's a tutorial that solves your problem: [link](http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell/) Hope it helps someone. – Priest Oct 07 '15 at 09:34
  • How is collectionview cell loaded to the table view?? – Rashid KC Jul 04 '17 at 07:10
  • @TheBasicMind i have scrolling problem in collection view if the number of item in each collectionview is different.Any help will be appreciable – Ahmed Sahib Jul 15 '17 at 04:37
7

This is a pretty common issue that I've needed to solve multiple times, and I've created a UICollectionViewLayout that solves this in a clean way without creating multiple collection views, meaning it supports moving items between sections etc.

https://github.com/joelekstrom/JEKScrollableSectionCollectionViewLayout

Accatyyc
  • 5,798
  • 4
  • 36
  • 51