7

I'm working on an iOS 5 project with storyboards, and thus using dynamic table cell prototypes in IB. In one of the views, I've got a table view with variable-height cells, with the cell height calculated from the height of the contents.

tableView:heightForRowAtIndexPath: returns correct values for all cells when the table view is first displayed. All is well when scrolling down for a few items, but then something goes amiss: the actual cells seem to be correct height, including their touch areas, but their separators are rendered in the wrong places (inside cells instead of between them).

From measuring the separator placement, it appears as if cell reuse might have something to do with this. The separators for the first three cells are rendered correctly, but not the fourth one. heightForRowAtIndexPath: returns the correct height for it (125 pixels in this case), and its contained subviews are all in their right places. However, the separator is rendered only 108 pixels from the previous separator, placing it inside the 125 pixel high area of the cell.

Here's the kicker: 108px is the height of the first table cell, now out of sight and probably reused. I don't have definite proof of this, but it appears that the table view is ignoring heightForRowAtIndexPath: for these cells and is just rendering the separator according to the reused cell height.

This doesn't explain why a bunch of later, shorter cells are not rendered separators at all. But this is all I've got to go on.

Is there a workaround, an IB setting or something else that might help?

JK Laiho
  • 3,538
  • 6
  • 35
  • 42
  • I gave up. I ended up disabling the separators from the table view properties and added a 1px high view to the bottom of the cell prototype to act as a separator. Now it at least looks good. I'd still like to know why it misbehaved though, so answers are still more than welcome. – JK Laiho Apr 04 '12 at 06:07
  • Just having this issue with my variable-height tableview too... and I'm doing it all programmatically. Oh well, at least I didn't need the lines, but it's still confusing :/ – cclogg Oct 26 '12 at 07:22
  • Just confirming one thing, you are using the tableView separators or adding a line/UIView as separator? – Akshat Singhal Mar 14 '14 at 16:53

2 Answers2

22

I had the same problem where the separators were showing at seemingly random positions.

It turned out the problem was that I was overriding layoutSubviews but forgot to call [super layoutSubviews]. Adding that call fixed the issue for me.

pfluger
  • 221
  • 2
  • 4
1

I also met this weird problem. For a custom table cell, add a layer as a separator may be a better choice.

in initWith*** method of the custom table cell class:

separator = [CALayer layer];
separator.backgroundColor = [UIColor colorWithWhite:0.8f alpha:1.0f].CGColor;
[self.layer addSublayer:separator];

update separator's frame in layoutSubviews method:

- (void)layoutSubviews {
   CGFloat height = 1.0f;
   separator.frame = CGRectMake(0.0f, self.frame.size.height - height, self.frame.size.width, height);
   [super layoutSubviews];
}
lagrangee
  • 111
  • 1
  • 3
  • Upvoted as helpful, but I'll reserve the solved checkmark for now in case someone has an explanation of the behaviour itself. – JK Laiho Jul 11 '12 at 06:38