Calculate height of tableView height inside the collection view cell - ios

I used tableView inside the collectionView cell.My question is how to calculate the tableView height and set that height in collectionview cell.
NB: The table view height will be dynamic.
Please help me.

Since UITableView is a subclass of UIScrollView you can get its height by calling tableView.contentSize which will give you width and height of the table view's content

You can put KVO(observer) on contentSize of tableview, so whenever contentSize of tableview will get updated, you will get notified, at that time you can modify the height of collection view cell.

Related

Auto set height of table view based on UICollectionView inside it

I have a UITableView with cells that have inside a UICollectionView.
Now all works great except of UICollectionViewCell scrolling i want that the height of table view cell follow the Collection view cell content.
In simple word i want scrolling in table view but not inside single cell i need to display all contents of UIcollectionViewCell without scrolling inside it.
Thanks
You have to calculate the collectionviews' heights and set a height constraint on each of them. then add top and bottom constraints from the collectionviews to their superviews (the tableviewcells / their contentviews) and let autolayout do its work.
don't forget
tableView.estimatedRowHeight = 44 // or something similar
tableView.rowHeight = UITableViewAutomaticDimension
in the controller's viewDidLoad to make the automatic row height calculation work.

Change frame of UITableView as per number of rows in cellForRowAtIndexPath in Objective-c

I am resizing my UITableView as per its number of rows in cellForRowAtIndexPath:, that's why the next part of the UITableView was not visible, and for the part of the UITableView which is not visible, cellForRowAtIndexPath: is never called.
But I need to manage TableView frame according to number of rows, please suggest how can i implement this.
Thanks
If you are using autoLayout
To your UITableView add a leading, trailing, top and bottom constraint >=0. Also add a fixed height constraint with a priority of 750. Create an IBOutlet for this height constraint to your UIViewController. Now you can calculate your height using. This will allow your UITableView to only become scrollable once the total height exceeds your screen bounds.
//dataArray = array of your data source
tableViewHeightConstraint.constant = dataArray.count * rowHeight;
Now this height constraint will break as soon as the UITableView becomes scrollable so if you are adding the ability to delete the rows, you will have to create a new height constraint to the UITableViewas soon the number of rows of doesn't take up the entire screen bounds and update the new height constraint appropriately.
You can do using UITableView's content size property .
[tableView reloadData]; // you need to reload data first before set frame
tableView.frame =CGRectMake(tableView.frame.origin.x,tableView.frame.origin.x, tableView.frame.size.width, tableView.contentSize.height);

Dynamic Height for UITableview

i have a tableview with a dynamic changing number of cells. sometimes there are 10, another time more than 20 cells.
This Tableview is on a Viewcontroller, but i have so much content on this tableview, so my tableview and all other content are on a scrollview. now i have a double scroll. on on my scrollview, and another on my tableview and this is quite uncomfortable. so how can i set the height of my tableview dynamically?
If you are using Autolayout with storyboard then set constraints of your tableview
let Count = CGFloat(dataArray.count)
constraintsTblHeight.constant = Count*100 //100 is your tablview's cell height
other wise increase this height in tablview height

In table view I want to add a view its height should be dynamic according to cell height

In UITableview I am adding another UIView, it's height should be dynamic according to cell height, the problem I am facing is it is not accurate.
The height of UIView is not varying correctly with cell and it is calculated again and again on table reloading.
When you load your data, you can calculate the each cell's height, and store them in a array, then you can use this frame array to setup the uiview's frame
You have to provide constraints on your UIView according to contentView of cell top,bottom,leading,trailing value 0 , So your UIView height and width is same as cell height & width.
Please refer http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1 for learning autoLayout for beginner.

Custom prototype cell not increasing uitableview frame height

I have tableview frame height is 300. I have created one custom UITableViewCell subclass for adding to UITableView cell. When ever i adding custom cell to tableview, tableview is not increasing the height of frame. Tableview height constant like previous height 300. I want that whenever custom cell is adding the tableview height should be increase. How to resolve this?

Resources