当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - 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 UITableViewCellSelectionStyleBluewhen 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;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: