简体   繁体   中英

Tableview cell separating line disappears when tapped

I have a UITableView with custom tableview cells with the separator line between each cell. I recently began implementing multi cell selection when in editing mode. In order to have the blue circled checkmarks when each cell is selected, I changed cell selectionStyle from UITableViewCellSelectionStyleNone to UITableViewCellSelectionStyleDefault . To get rid of the gray shade when the cell is selected I just implemented a white background like this:

UIView * cellBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cellBackgroundView.backgroundColor = [UIColor clearColor];
cell.multipleSelectionBackgroundView = cellBackgroundView;
cell.selectedBackgroundView = cellBackgroundView;

The problem is that when I'm not in edit mode, the separator line disappears whenever the cell is selected, but when I select a cell in edit mode, the line remains. Any idea how to resolve this so the separator always remains?

try following:

Add this lines

[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView reloadRowsAtIndexPaths:@[indexPath]
                 withRowAnimation:UITableViewRowAnimationAutomatic];

at the begining of didSelectRowAtIndexPath

this is weird bug which exists since iOS7.0

I know that the cell separator disappearing is a common issue, but none of the current solutions seemed to solve my problem so I came to the conclusion that I would need to toggle my selectionStyle to UITableViewCellSelectionStyleNone when my tableView is not in editing mode and UITableViewCellSelectionStyleBlue when I'm in editing mode. I was able to achieve this by calling the following in my view controller containing my table view:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if(tableView.editing)
    {
        [((customTableViewCellClass*)[tableView cellForRowAtIndexPath:indexPath]) changeSelectionStyle:YES];
    }
    else
    {
        [((customTableViewCellClass*)[tableView cellForRowAtIndexPath:indexPath]) changeSelectionStyle:NO];
    }

return indexPath;
}

Then in my custom tableviewcell class I call the following:

-(void) changeSelectionStyle: (BOOL) selected
{
    if(selected)
    {
        self.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    else
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }

}

In cellForRowAtIndexPath I left the code that changed the view for multipleSelectionBackgroundView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView * cellBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cellBackgroundView.backgroundColor = [UIColor clearColor];
    cell.multipleSelectionBackgroundView = cellBackgroundView;
    cell.selectedBackgroundView = cellBackgroundView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM