dynamic, custom layout UICollectionView embedded in UITableViewCell - ios

I have UITableViewController with 1 section and header with UISegmentedControl inside.
If user presses first segment we should display UITableView cells
If user presses second segment we should display custom vertical scrolling collection grid with collection items below that header
possible solutions:
change UITableViewController to UICollectionViewController, and UITableViewCells to UICollectionViewCells. This should work but I'm trying to avoid this solution
UICollectionView embedded in UITableViewCell. Setting UICollectionView scrolling enabled to false, adjusting UITableViewCell's height to fit entire UICollectionView's contentSize.height. In systemLayoutSizeFitting(...) method of container (UITableViewCell) we call layoutIfNeeded on container and on contained UICollectionView and return UICollectionView's contentSize.
problem:
(re)calculation of entire UICollectionView contentSize requires a lot of work if we have hundreds of items (cellForItemAtIndexPath for every item). Want to avoid this somehow. Maybe we can increase container (UITableViewCell) height during scrolling UICollectionView which is embedded inside.
Same as 2 but we have fixed size container UITableViewCell (same height as view without header) with UICollectionView inside. When we scroll UITableView to the very bottom UICollectionView scroll should continue scrolling.
problem:
I don't understand how to deal with 2 instances of UIScrollView with scrollingEnabled = true for both. I think this could be solved with pan gesture recognizer delegates. If it is so I need help here.

I would personally opt for number 1 and completely avoid number 2. For number 3, you could set tableView.scrollingEnabled = false when the second segment is tapped. Set it to true when the first segment is tapped. That way, when it's false, the UICollectionView in your UITableViewCell can still scroll.
This tutorial may help with putting a UICollectionView in a UITableViewCell.

Related

Scroll Multiple CollectionViews simultaneously

I have a tableView with 20 tableViewCells and within each cell I insert a collectionView (with 100 cells) which scrolls horizontally. I would like to scroll ALL collectionViews when any of the collectionViews is scrolled.
Implementation
I have subclassed the collectionView and have overridden scrollView delegates.
I have currently gotten this to work with the following approaches:
When ANY CollectionView is scrolled, I get all visible tableViewCells and grab the collectionsViews within each cell and set the collectionView contentOffsets iteratively or by using makeObjectsPerformSelector.
Had a CGPoint property with an observer which triggers when the property changes; then inside the collectionView subclass scrollViewDidScroll: I set the property, this will trigger the observer and set the collectionView accordingly. (the collectionView contentOffset is set inside observeValueForKeyPath:ofObject:change:context:)
I have also used NSNotificationCenter to broadcast any scroll and in-turn scroll every other listener
The problem I face is that the collectionViews scrolls are a bit glitch sometimes especially when the scroll is coming to a halt. currently there is no data in collectionView cells but it glitches.
Please does anyone know of a way to simultaneously scroll multiple collectionViews without glitches.
Thanks in advance.

AutoLayout setup for a UITableView within a UITableViewCell

I want to show multiple labels in my main UITableViewCell, for which I am trying to use a UITableView within the cell. Now I have setup all the delegates and datasources properly, and the data is coming in right. But the inner UITableView has a dynamic number of cells, and a dynamic height respectively.
What I want is, to have an autolayout constraints setup, which allows my main UITableViewCell to resize as per it's inner UITableView.
Currently what's happening is, the inner tableView loads data perfectly, but does not resize as per the data, due to which the main cell can also not resize.
Any help in this regard would be appreciated.
Autolayout can not calculate the height of uitableview that inside cell because uitableview scroll inside there fore you can calculate the heigth required for your uitableview According to no. Of rows and content and then set this calculated height . in heightForRowAtIndexPath for main cell .

UIScrollView in Custom UITableViewCell mixing up data

I have a UIScrollView in each UITableViewCell of my table view that lays out UIViews horizontally - kind of like the "Featured" section on Apple's App Store. When I'm setting up the UITableViewCell I call a function within the custom UITableViewCell to layout the scroll view. This loops through data assigned to that tableview cell's index path and then creates the custom views and adds them to the scroll view. However, these get mixed up when scrolling the tableview and when the tableview refreshes.
If I clear the subviews before laying them out, it does work. However, I'd like to keep the scroll position at the same point every time it shows the cells. How is this possible?
I've just added an external array that stores the current offset for each scrollview, and then manually set the offset on each scrollview everytime the tableview gets refreshed.

AutoLayout. Stick UITableView to the bottom of UIScrollView

I need to achieve layout shown below. There are UIView and UITableView inside UIScrollView:
UITableView should be aligned right at the bottom of UIScrollView
UITableView should not have scrolling, so it's height should be adjusted according to content.
Row count is not static value.
Here are the constraints of UITableVIew:
If I understand AutoLayout correctly, everything should be fine, but as result I get UITableView's height to be 0. What's wrong with this setup?
I don't know what you are displaying above the UITableView but the normal setup for displaying something, that is above the tableView and scrolls with the tableview would be to use a UITableView (It is a Subview of UIScrollview and thus handles the scrolling itself) and add the things that you want to display above it as a tableHeaderView to your tableView.

UICollectionViewCells disappear when going out of UICollectionView frame

Consider the following:
I have a UICollectionView with a size of 400x400. This UICollectionView has 5 UICollectionViewCells which also have a size of 400x400.
Clip to bounds is set to false so that my UICollectionViewCells will be visible (this does not work, I can only see 1). I want this so I can use paging. When I start scrolling there are 2 cells, but when the scrolling snaps, the previous cell disappears and I can only see 1 cell.
I use dequeueReusableCellWithReuseIdentifier to retrieve cells to fill the collectionview. Can this be part of the problem?
How can I display the UICollectViewCells that are out of the collection view's frame?
You can't. What you describe for a problem is actually a feature of UICollectionView and UITableView. They reuse the cells that are not visible for performance and memory reasons. You might think of another approach like UICollectionView with custom layout if that would be possible for your requirement or directly use UIScrollView with paging and you rearrange all the views while is scrolling.

Resources