2

(iOS 8 with Storyboard) I have a right detail UITableViewCell on my tableView.

In storyboard the Title and Right Detail labels are set to numberoflines = 0

My Code:

- (void)viewDidLoad {
    [super viewDidLoad];
    keyArray = [[NSMutableArray alloc]init];
    self.table.estimatedRowHeight = 140.0;
    self.table.rowHeight = UITableViewAutomaticDimension;
    [spinner startAnimating];
    [self cleanJson:prunedDictionary];

}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;

}

At the end of cleanJson I run [self.table reloadData]. The TableViewCell does resize but they all resize to the same size and it seems like the default size. (The reason I know they resize is because in Storyboard I set the height to 200 and they were all resized to a smaller size when the app ran)

Why aren't they resizing correctly?

2
  • Your constraints are likely wrong. Can you add them to your question somehow? Commented Oct 30, 2016 at 12:55
  • @d.felber No constraints since as I mentioned, its not a custom cell. Therefore it already has constraints as far as I know Commented Oct 30, 2016 at 12:56

3 Answers 3

3

The problem is that you are using a built in cell type and the built in title and detail labels. Those cell types are not self sizing.

You must use a custom cell and design it yourself, with constraints that size the cell from the inside out.

Sign up to request clarification or add additional context in comments.

Comments

0

You do not need any of those two delegate methods. The cells do not resize properly because your auto layout constraints setup is incorect.Check those constraints and make sure there exists at least one constraint-view path from top to bottom. By that I mean you are able to logically hop from top of the cell itself..via all the views and vertical constraitns between then down to the bottom.

like this:

top of superview

margin constraint

view

margin constraint

.

.

.

.

view

margin constraint

bottom of superview.

The views in the between must either be constrained to fixed size ..or they must abe able to resize according to their content. It's clear that the final height is a combination of all those margins and view heights.

It's a linear function

finalheight = topmargin + viewHeight + margin + viewHieght..etc....etc..bottommargin 

4 Comments

It's not a custom cell. So constraints are already set I believe
Can you verify it's really true? Assumptions are the single worst enemy in debugging.
That its not a custom cell? I can verify that yes. That custom cells do not require constraints. Almost positive.
That the constraints are in place and working. Trust nobody, certainly not UIKit. You can use Live View Debugger.
-1

There is actually a way - but I would prefer creating a custom UITableViewCell:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let cell = self.tableView(tableView, cellForRowAt: indexPath)
    let targetSize = CGSize(width: self.tableView.bounds.width, height: CGFloat.infinity)

    return cell.systemLayoutSizeFitting(targetSize,
                                        withHorizontalFittingPriority: 1000,
                                        verticalFittingPriority: 900).height
}

1 Comment

doesn't work now Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSLayoutConstraint constant is not finite! That's illegal. constant:inf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.