Sunday, July 3, 2016

iOS UITableView keep the separator line visible when the cell is selected

Usually when we select the first cell in a tableView on iOS, the separator line between the first row and second row disappears. This is annoying, and usually the UI design recommends the separator line to be there always. The following code fixes the issue. It adds a view as the separator line between cells. 

Remember we have to keep the UITableView's selection style as UITableViewCellSeparatorStyleNone and separator style as UITableViewCellSelectionStyleNone



- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    UIView *selectionBgView = [[UIView alloc] init];
    selectionBgView.backgroundColor = [UIColor colorWithRed:245.0/255.0f green:242.0f/255.0f blue:241.0/255.0f alpha:1.0];
    cell.selectedBackgroundView = selectionBgView;

    UIView* separatorLine = [[UIView alloc] initWithFrame:CGRectMake(0, BOX_TABLE_ROW_HEIGHT-1, boxesTableView.width, 1)];
    separatorLine.backgroundColor = COLOR_GRAY_212;
    [cell.selectedBackgroundView addSubview:separatorLine];
    return YES;

}

No comments:

Post a Comment