Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView separator lines disappear between cells on scroll

Problem: The separator between cells in a table view appear only for those cells shown when the view loads, and only at load time. When the tableview is scrolled down, the cells scrolled into view show no separator between them, then when the tableview is scrolled back up, the initial cells show no separator.

Details: I've got a UITableView to which I'm adding standard UITableViewCells. These cells are created with initWithFrame, frame height = 90px. I'm adding a custom view created from a nib to this cell's view, height = 90px. The cell height is specified at 90px in tableView:heightForRowAtIndexPath:.

Has anyone experienced this behavior?

like image 202
jtrim Avatar asked Oct 08 '09 18:10

jtrim


4 Answers

I had a feeling the solution to this would be simple... I made the height of my cells 91px and the separator lines appear as they should on scroll.

like image 111
jtrim Avatar answered Dec 24 '22 20:12

jtrim


I couldn't use Douglas's solution because my tables have a huge amount of cells and would become pretty much unusable on older phone. Reusing cells is key for performance.

BUT, I managed to workaround the problem using a transparent separator and adding my own in the contentView of the cell, as follows:

yourTable.separatorColor = [UIColor clearColor];
separatorView.frame = FactRectMake(0, rowHeight-1, appFrame.size.width, 0.2);
like image 25
Marc Provost Avatar answered Dec 24 '22 18:12

Marc Provost


I had the same problem, but I used a different solution. My separators were disappearing because I was clearing my cell using:

for (UIView *eachView in self.subviews) {
    [eachView removeFromSuperview];
}

This removed the separator view as well!

Instead, I assigned a tag for each of my customs views (three labels) right before adding them to the sub view:

tempFirstNameLabel.tag = 100;
self.firstNameLabel = tempFirstNameLabel;
[self addSubview:self.firstNameLabel];

Then when I cleared the cell, I just removed those views:

for (int i = 100; i<103; i++) {
    UIView *eachView = [self viewWithTag:i];
    [eachView removeFromSuperview];
}

Hope this helps! This also avoids the memory management issues that @Douglas Smith's solution posed.

like image 42
Tim Avatar answered Dec 24 '22 19:12

Tim


You should set separator none and then single line again

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // it is a bug in iOS 7
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
like image 21
Mihriban Minaz Avatar answered Dec 24 '22 18:12

Mihriban Minaz