AutoLayout setup for a UITableView within a UITableViewCell - ios

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 .

Related

Collectionview inside tableview cell with dynamic height cells and dynamic content swift 4

I am working with collectionview inside tableviewcell in Swift 4. I get the content from server which is all dynamic. I have to show different types of collectionview behaviours.
For example, in the first tableview row should be collectionviewcell with screenwidth and scrollable. In the second row, collectionview with 3 sections and each section has different content different number of items.
Here I got stuck I am unable to show 3 sections one below other it is showing besides horizontally. I have calculated collectionview flow layout size.
Also in last row I have collectonviewcell with scrollable content,here when i scoll the content is repeating from bottom row or top row. I want the smoth scrolling and to stop repeating the content.
I am new to Swift. Can anybody help me out of this . Thanks in advance.
The smooth way is to create tableview with its cell xib and inside its cell add collection view and create another xib for collection view cell. This method is helpful when accessing collection view for different tableview cell. Rather using without loading Xib's may mixup your code.
You have to set tableview cell height as UITableViewAutomaticDimension. This will handle your cell height as per your dynamic content.
For each tableview cell you have to set collection view's property by using cell's index path as per your requirement.

dynamic, custom layout UICollectionView embedded in UITableViewCell

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.

How to scroll the UICollectionview from particular cell

I have a collection view. I need to scroll only cell4, cell5, cell 6 so on. First two cells are remain same. Is is possible using only UICollectionview. How to achieve this.
FYI: the height of the cell is more than the screen height.
One of options would be to subclass UICollectionViewLayout and update frames in attributes of first three cells every time when scroll offset changes. So the cells would look static.

How to make a particular static cell in UItableview self adjusting according to the constraints of the contents inside it

I've done some research on self adjusting static cells but they seem to apply to the entire table view as a whole.
what if I'm only interested in targeting a specific cell like the picture below? How do I go about doing it?
edit: What I intend is for the tableview cell to expand when the collectionview gets populated with more cells. Thus allowing the entire page to scroll accordingly.
Are the images in the collection view always the same height ? If the images in the view are a static size, you could just divide the number of images, by the number that will fit on a line. Then multiply that by the height of the lines to set the tableview cell height.
This might be helpful:
Dynamic Sized Tableview Cells

How to get height of UITableView placed in another UITableView in Cell as childViewController

I'm struggling with wired kind of problem. I have UITableView with one cell. This cell is only holding containerView with childViewController (second tableView). My problem is that, first tableView(parent) must have UITableViewAutomaticDimension row height but it doesn't work (It dosen't know correct size of that cell with second tableView).
How to get correct size of second tableView) ?
Second tableView (inside Cell) have scrollingEnabled turned off (tableView.scrollEnabled = false), first tableView must have correct size of second tableView in order to provide correct scrolling experience.
Since a UITableView inherits from UIScrollView you can use the contentSize property to get this information. This is how the scrollbar works on the side.
I have done a similar thing before (long ago), so the following is somewhat hazy/ irrelevant/ unessecary. There might be some gotchas with unknown/unrendered cell heights in the embedded tableview. I rememeber having to set the tableview height to a large number (forcing all cells to render) fetching the contentSize then resetting the tableview height to the contentSize.height.
Are you shure you really need second table view? If you have just one cell probably you don't need it. I recommend you to calculate table view height as the summ of it's cells heights. If cells of embedded table view have constant height it could be simple. In other case you also could calculate cells height.

Resources