I have a UITableView that has five static cells. I need the cell height of one cell to adjust automatically to its contents, which is one UILabel.
Is there any way I can use..
tableView.estimatedRowHeight = 42.0
tableView.rowHeight = UITableViewAutomaticDimension
..for a table view with static cells, or does this only work for table views with dynamic prototype cells?
(Or is there any other way of doing this that you would recommend?)
The UITableView is in a TableViewController which is embedded into a Container View.
The five static cells are quite different from one another and are only used in one app scene, so I don't see much point in going for dynamic prototypes and custom cell subclasses.
I update the tableview like this
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[_myTableView reloadData];
}
I add constraints to the UILabel as described here: http://www.appcoda.com/self-sizing-cells/
For iOS 8, I found out that you cannot do the same strategy as for Dynamic Table View Cell.
To automatically adjust the height of static cells, I implement these two UITableViewDelegate methods:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
Hope it helps.
Expanding over Victors' answer, Static Cells based table views seem to autosize cells based on content and constraints once the following delegates are implemented:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
EXCEPT for when there are labels. For some reason the label intrinsic content heights do not seem to contribute towards the height calculation of the cell when they have . My current work around for this is to nest the labels in an UIView.
Do the following:
Certainly feels like a hack but works for me in all the cases I have tried.
Ended up doing this. Not what I wanted, but it kind of works for my simple requirements and text lengths.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 2)
{
CGFloat padding = 30.0f;
CGFloat labelWidth = self.tableView.bounds.size.width - padding*2;
NSAttributedString *text = [[NSAttributedString alloc] initWithString:_freeTextLabel.text];
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin;
CGRect boundingRect = [text boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX)
options:options
context:nil];
return (CGFloat) (ceil(boundingRect.size.height) + padding*2);
}
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With