I have the a custom tableview cell with applied constraints but the first time the table is displayed the row height is not resized properly unless new cells are created, is there a way to do this without calling reloadData again?
-
Did u return the correct height in the heightForRowAtIndexPath delegate? post some code. – Ramaraj T Aug 18 '15 at 05:24
-
@RamarajT I'm using the rowHeight property – Angie Aug 18 '15 at 13:59
1 Answers
Yes. This is actually an issue with self-sizing that you need to work around until it is fixed.
The problem is that when a cell is instantiated, its initial width is based on the storyboard width. Since this is different from the tableView
width, the initial layout incorrectly determines how many lines the content actually would require.
This is why the content isn't sized properly the first time, but appears correctly once you (reload the data, or) scroll the cell off-screen, then on-screen.
You can work around this by ensuring the cell's width matches the tableView
width. Your initial layout will then be correct, eliminating the need to reload the tableView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
[cell adjustSizeToMatchWidth:CGRectGetWidth(self.tableView.frame)];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
In TableViewCell.m:
- (void)adjustSizeToMatchWidth:(CGFloat)width
{
// Workaround for visible cells not laid out properly since their layout was
// based on a different (initial) width from the tableView.
CGRect rect = self.frame;
rect.size.width = width;
self.frame = rect;
// Workaround for initial cell height less than auto layout required height.
rect = self.contentView.bounds;
rect.size.height = 99999.0;
rect.size.width = 99999.0;
self.contentView.bounds = rect;
}
I'd also recommend checking out smileyborg's excellent answer about self-sizing cells, along with his sample code. It's what tipped me off to the solution, when I bumped into the same issue you are having.
Update:
configureCell:forRowAtIndexPath:
is an approach Apple uses in its sample code. When you have more than one tableViewController
, it is common to subclass it, and break out the controller-specific cellForRowAtIndexPath:
code within each view controller. The superclass handles the common code (such as dequeuing cells) then calls the subclass so it can configure the cell's views (which would vary from controller to controller). If you're not using subclassing, just replace that line with the specific code to set your cell's (custom) properties:
cell.textLabel.text = ...;
cell.detailTextLabel.text = ...;

- 1
- 1
-
Thanxs for the response!! I'm using ios8 and can get this line to work [self configureCell:cell forRowAtIndexPath:indexPath]; – Angie Aug 18 '15 at 14:15
-
I've updated my answer to explain that line. If you don't subclass `UITableViewController` and call its superclass, you won't need to use any approach like that. What really matters is that once you set the cell's width **in advance of** setting the labels or other custom controls, the cell will be able to initially self-size itself correctly. – Aug 18 '15 at 18:18
-
Tried your solution and it did not worked, does it has to do something with the version of ios8? – Angie Aug 22 '15 at 03:12
-
-
It will work fine with 8.4. When the initial layout is incorrect, it's because the cell width is wrong, which causes the labels to initially be laid out wrong. (After reloading, the cell's width is right, so the layout is correct.) Make sure you set the cell's width, either in `cellForRowAtIndexPath` or in your custom cell's code (like I did), and you should be fine. – Aug 22 '15 at 20:15
-
will you know how to do this? http://stackoverflow.com/questions/32152274/uitableviewcell-height-with-uitextview-not-re-sizing-correctly-ios-8-4 – Angie Aug 25 '15 at 03:52