Reputation: 245
I explored the existing Q&As on SO about this question but did not find my answer.
I know this is caused by tableview not knowing the height of the custom cell at run time,but not sure how to get over this. This is iOS 8 + Xcode 6. I did all the auto layout required methods for intrinsic size of the custom cell...
Standard tableview with standard cells, only one (row = 2) created in code as custom cell;
customCell:
-(CGSize)intrinsicContentSize
{ //hardcode for testing purposes
return CGSizeMake(500.0, 450.0);
}
when running on iOS simulator, found that the customCell is displayed "underneath" other cells below its row, with height standard, the same way other standard cell height is set, instead of its height set much bigger than other cells.
In table view controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
pageViewCellTableViewCell* customCell;
if (indexPath.row != 2)
{
cell = [tableView dequeueReusableCellWithIdentifier:@"regularCell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"Table Row %lu", indexPath.row];
return cell;
}
else
{
customCell = [tableView dequeueReusableCellWithIdentifier:@"customCell"] ;
if (customCell == nil) {
customCell = [[customCell alloc] init];
}
customCell.parentViewController = self;
[customCell setNeedsUpdateConstraints];
[customCell setNeedsLayout];
[customCell setNeedsDisplay];
return customCell;
}
return nil ;
}
- (CGFloat)tableView: (UITableView*)tableView EstimatedHeightForRowAtIndexPath: (NSIndexPath*) indexPath
{
if (indexPath.row != 2)
return 10;
else
return 450;
}
- (CGFloat)tableView: (UITableView*)tableView HeightForRowAtIndexPath: (NSIndexPath*) indexPath
{
if (indexPath.row != 2)
return 10;
else
return 450;
}
When running on simulator:
Got the following Error msg: Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
Upvotes: 3
Views: 8048
Reputation: 411
I was struggling with the UITableViewCell getting compressed vertically using 2 Labels.
Setting bottom spacing to the container as mentioned by Mike C. - did the trick as well for me.
Right click and dragged from the left side of the label bounding box (outer label bounding box, not the label name itself) to the lower left of the Content View opens the Constraint view. Selected "Bottom Space To Container" - Voilà.
Upvotes: 0
Reputation: 1311
You can use self-sizing cells in iOS 8 (Swift code):
tableView.estimatedRowHeight = 88.0
tableView.rowHeight = UITableViewAutomaticDimension
Two tutorials list below:
Upvotes: 4
Reputation: 1682
I just dealt with this as well. I'm doing a custom tableViewCell from a nib file instead of using the storyboard cell. I added the code (Objective-C) as suggested above:
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView setEstimatedRowHeight:85.0];
Didn't help. What did help was fixing my constraints. I assumed that by pinning to the top, and then just setting the vertical spacing between the next two objects would be enough. I had also set a fixed height constraint for the UILabels thinking that would help. It didn't. Removing any fixed-height constraints and pinning the bottom object to the bottom of the container resolved the issue for me. Turns out that auto-layout is smarter than me. Who knew!? lol
Upvotes: 2
Reputation: 167
I faced a similar error couple of days ago. I suggest you to check the Auto Layout constraints once more.
When setting the tableView.rowHeight = UITableViewAutomatic
you have to make sure that each element in the cell has a leading,top & bottom constraints. Else swift can't change the height dynamically based on content size.
Upvotes: 6
Reputation: 245
Figured it out.
According to the WWDC 2014 video "What's new for Tableview and Collection View":
Upvotes: 2