Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView separator strange behavior

I have a plain style table, with multiple sections.

Symptoms: when the table is first loaded, each cell has it's separator as normal, except the last cell in each section (that is "before" a header). However, when there is scrolling, on some cells the separator appears. Also, if a cell is selected, then deselected, the separator appears.

Printing the view hierarchy shows, that on clean start, the separator view of the last cell in each section is hidden, so I would assume that would be the normal behavior:

<_UITableViewCellSeparatorView: 0x145b1990; 
    frame = (0 47.5; 320 0.5); 
    hidden = YES; 
    layer = <CALayer: 0x145b2950>>

On scrolling sometimes; and by selecting the cell; this hidden property gets removed, and so the separator appears.

First assumption was I was doing something wrong; but this happens with the most basic hello world application too:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView 
    numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView 
    titleForHeaderInSection:(NSInteger)section {
    return @"test";
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *reuseIdentifier = @"TestCell";
    UITableViewCell *cell = [tableView 
        dequeueReusableCellWithIdentifier:reuseIdentifier];
    return cell;
}

Screens (left: after start; right: after selecting a cell):

startafter selecting a cell

Is this a bug on Apple's side, or am I missing something? Also, for example, in the iOS Contacts application the separator is never hidden above a section header...

**Update: **

Was able to reproduce this in stock Music app: e.g. Songs tab, no spearator. Select then deselect first cell of any section; separator appears.

Extra info: iOS 7.1

like image 406
Robert B. Avatar asked Mar 23 '14 10:03

Robert B.


1 Answers

Since this is an iOS 7.1 bug, as a workaround I would suggest hiding the built-in separator and showing one of your own at the bottom of each cell.

Hiding the built-in separator:

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

Showing a custom separator:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *reuseIdentifier = @"TestCell";
    XXYourCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    cell.customSeparatorView.hidden = (indexPath.row == ([self numberOfRowsInSection:indexPath.section] - 1));
    return cell;
}
like image 65
Tom Susel Avatar answered Oct 08 '22 09:10

Tom Susel