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;
}
Related
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.
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.
I have a custom view inside a prototype cell, with a label and a UIImageView that can both be variable heights. What is the best approach to get the table cells to adjust to the view's height? It seems I have to set a height to the table cell and anything else I try won't reset the size.
i've tried the concept below with no luck.
tableView.estimatedRowHeight = 260.0
tableView.rowHeight = UITableViewAutomaticDimension
As far as I know, the height for UITableViewCell should be fixed. You could have more than 1 type of UITableViewCell inside the same UITableView. For example, the section 0 has a unique type of UITableViewCell, the UITableViewCell for the other sections have another type. Then inside the TableView Delegate method heightForRowAtIndexPath, you may set the height for the different types of UITableViewCell.
You could do something like this:-
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section==0)
return 40;
else
return 60;
}
I'm trying to put the Flowlayout into TableViewCell content.
Everything works fine, but I want to setup dynamic row height based on FlowLayout height.
Using the next method we can get need size. But when I setting it to value that is not equal "44" - FlowLayout stops to show its content. It works only with 44 px.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44.0;
}
;
Changing row height via storyboard produces the same result. So Is it possible to contorl the size of nested FlowLayout?
There is an article on cimgf.com Please see: http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/
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.