UITableView Rows Height After Loading Cells - ios

I have UITableView that contains 4 different types of customized cells in storyboard. Each cell has customized UILabels which get variable amount of text data from backend. I am struggling with making the cells resizing correctly. I would really want to change the height of each cell but I can not use heightForRowAtIndexPath because it is called before cellForRowAtIndexPath, but the height is actually calculated within each customized cell.
I tried writing in each cells' height into an array while the UITableView is loading, then just reloading it all over again once, but no effect. I tried using CGFloat rowHeight = UITableViewAutomaticDimension with no success either. The customized labels in each cell definitely grow with text which I see when I just statically change row height to higher numbers. So, I would need somehow my labels to push on rows to make them grow, not sure.
Different similar posts on stackoverflow that I found did not help.

The issue was that I needed to set up top and bottom constraints to the ContentView and NOT to the cell itself in the storyboard.
Label -> ContentView top and bottom constraints need to be set up. And then UITableViewAutomaticDimension specified in viewDidLoad:
self.tableView.rowHeight = UITableViewAutomaticDimension;
estimatedRowHeight should be set too. For example:
self.tableView.estimatedRowHeight = 76.0f;

First Method called is:
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
Second:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Then:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
set a break point in the above methods and test it. So if you want to preset the height use estimatedHeightForRowAtIndexPathmethod.

Related

How to increase UITableView Height Dynamically When number of cell is increased using Objective C?

I am new to iOS. I have implemented a table view which is populated with array values successfully, but I'm not satisfied with the result. When I scroll my table view the top values are hiding.
Here is my problem: based on the cell height the table view height should also get increased dynamically in UITableview. How can I accomplish that?
For UITableViewCell dynamic height you can use following delegate by return UITableViewAutomaticDimension
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 300; //give maximum heigh you want
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
and to increase tableview height based on content size use the below line of code
YOUR_TABLE_VIEW_HEIGHT_CONSTRAINT.constant = [YOUR_TABLE_VIEW contentSize].height;
Hope this will help you

iOS Dynamic Table View Cell Height Without UILabels

Is it possible to use this feature without using text related views? This would be to not have to calculate the height, but instead let the cell self-size using subviews with constant height constraints, and eventually changing those height constraints at runtime.
The proper way to handle dynamic cell height changes in a table view is through tableView:heightForRowAtIndexPath: in UITableViewDelegate. You can grab the height from the subviews but you have to link that value to the index path of the cell.
Yes,you could build a tableviewcell with dynamic height.
1.use constraints to configure your tableviewcell's subviews
2.add a line of code In viewdidload method:
self.tableView.rowHeight = UITableViewAutomaticDimension
3.in some cases you must add another line of code In viewdidload method:
self.tableView.estimatedRowHeight = 40 //or any value other than your tableviewcell's height in IB.
Yes you can do that.
First Add constraints to subviews and add the following code in .m file.
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}

How to get iOS tableview last right content size with use auto layout

I have a tableview UITableViewAutomaticDimension for auto layout, and I get the tableview content size to make the tableview height as it content height after tableview cell configure and reload, this height isn't right it seems calculate by estimatedRowHeight. I know I can get the last right height use kvo to observe the tableview content size, but I don't know which is the last right one, because I just want do something once when content size is right. How can I do that?
you need to add a delegate method like
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
before that you should pin 4 sides on that contents which you dropped in prototype cell.
now you got the exact row heights of each cells.

UITableView: last cell is not completely visible

In my custom UITableViewCell I set the height of row as
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
As a result (I believe), when data is rendered, the last cell is not rendered correctly.
This happens on all views where I set the heightForRowAtIndexPath. How do I fix it, so that I can see last cell as well?
You are returning the height of the cell correctly and this issue seems to be related with table view and not with the cell, I think you need to reduce the height of the tableView.
If you are using AutoLayout then you need to set constraints to your tableView with Respect to its superview.
Else if just set
[tableView setAutoresizingMask:UIViewAutoresizingMaskFelxibleHeight | UIViewAutoResizingMaskFlexibleWidth];
Perhaps your UITableView's height is not right, check it.

How to increase the height of custom cell in UITableView

I have a tableview that fill with custom cells,, Now I want to increase the width of the cell,I increased the cell height of UITableview. But the problem is still same. Any one know how to increase the height of custom cell in UITableView.
Thanks,
If your cell heights vary, implement the following delegate:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44.0; // edit this return value to your liking
}
If they don't, merely set the rowHeight property of your table view instance to whatever height you want all of your cells to be. This answer does a good job of explaining why.

Resources