Are UITableView delimiters drawn incorrectly when reusing cells?

I am working on an iOS 5 project with piped versions and thus using dynamic prototype table cells in IB. In one view, I have a table view with cells of variable height, with the height of the cell calculated from the height of the content.

tableView:heightForRowAtIndexPath: returns the correct values ​​for all cells when the table view is first displayed. All is well when several elements are scrolled, but then something is wrong: the actual cells seem to be the correct height, including their touch areas, but their separators appear in the wrong places (inside the cells, and not between them). A.

From the measurement of the placement of the separator, it seems that reusing the elements may have something to do with it. Separators for the first three cells are displayed correctly, but not the fourth. heightForRowAtIndexPath: returns the correct height for it (in this case 125 pixels), and the subviews it contains are in the right place. However, the separator is displayed only 108 pixels from the previous separator, placing it in the high-resolution area of ​​125 pixels of the cell.

Here's the kicker: 108px is the height of the first cell of the table, now out of sight and probably reused. I have no definite evidence of this, but it looks like the table view ignores heightForRowAtIndexPath: for these cells and just displays the separator according to the cell height again.

This does not explain why a bunch of later, shorter cells are not displayed at all. But that’s all I have to go on.

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

+7
source share
2 answers

I had the same problem when separators showed up at seemingly random positions.

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

+22
source

I also met this strange problem. For a custom table cell, adding a layer as a separator might be a better choice.

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

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

update separator 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]; } 
+1
source

All Articles