2
votes

I have a UITableView configured as 'UITableViewStylePlain' with UITableViewCellSeparatorStyleSingleLine for its separator style. The cells have a background color.

The problem is that when you scroll the tableview, once some cells disappear off screen and are brought back, the separator is no longer visible.

The cells are registered with:

[tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];

Cell code:

- (void)customizeMyTable
{
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    NSString *cellIdentifier = [MyTableViewCell cellIdentifier];
    UINib *nib = [UINib nibWithNibName:cellIdentifier bundle:nil];

    [self.tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
    self.tableView.rowHeight = 50;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellID];

    [cell configure:someDataForThisRow];

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor lightGrayColor];
}

Anyone experience this problem? This seems to happen only on iOS 5, not on iOS 6 Tableviews.

7
show tableview's delegates. -(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath - NeverBe
Make sure the tableView's rowHeight is the exact height of your cells, or your cell heights exactly match the heights returned in the rowHeight function (in your delegate). - yuf
Even I'm facing the same problem. Have you found a solution for this? None of the solutions here worked for me. I'm facing this issue across all versions of iOS upto 11.x - DGoogly
I actually faced a peculiar scenario. The separator was removed when the Database returns empty array. So Even after re-adding the separator when database not nil, the separator gone missing or there appears two separators. But I found that it was because, the Custom cell I used had a Container view with Background color as Grey. So I modified it to have White. So too check with your Custom cell's contents and their properties. - DGoogly

7 Answers

12
votes

If you implemented layoutSubviews in your custom cell, and you accidentally forget to call the corresponding super method, you will not see the separator line.

Maybe you should check to see if you forget to call super in your other methods.

- (void)layoutSubviews
{
    [super layoutSubviews]; //<-THIS LINE YOU NEED

    //own code continues here
}
1
votes

I've had to set the separator insets to 0 to stop the problem.

1
votes

My problem also got solved by setting separator insets value 0.

1
votes

If you build content of your UITableViewCell dynamically, please be sure that you add your subviews to contentView not to self:

contentView.addSubview(subView)

instead of

self.addSubview(subView)

0
votes

It is related to LED screen resolution, since actual devices has very high resolution, the Monitor finds it hard to render those minute lines on the screen. It will work fine on actual device. someone told me not to care of it. so you are fine with this if problem is only with simulator.

0
votes

If its annoying to you then make a custom cell and put a separator into that and also remove the default separator from the cell by For Obj.C: [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

Otherwise you should be fine when you will test on real device.

0
votes

I have the same problem in swif. In my case, I found that the cells newly appear while scrolling overlap the others a little bit by clicking "debug by View Hierarchy" button. That's why separator lines disappear. So the solution is quite simple, set the cell return in cellForRowAtIndexPath clipsToBounds = true so that it would not go out of the height you assigned and overlap the other cell's separator line.

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellView = tableView.dequeueReusableCell(withIdentifier: cellReuseId, for: indexPath)
    cellView.clipsToBounds = true //Add this line
    return cellView
}