繁体   English   中英

轻按时,Tableview单元格分隔线消失

[英]Tableview cell separating line disappears when tapped

我有一个UITableView带有自定义tableview单元格,每个单元格之间都有分隔线。 我最近在编辑模式下开始实现多单元格选择。 为了在选中每个单元格时都带有蓝色圆圈的选中标记,我将单元格selectionStyleUITableViewCellSelectionStyleNone更改为UITableViewCellSelectionStyleDefault 为了在选择单元格时摆脱灰色阴影,我只是实现了白色背景,如下所示:

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

问题是,当我不在编辑模式下时,只要选择了单元格,分隔线就会消失,但是当我在编辑模式下选择单元格时,分隔线仍然保留。 任何想法如何解决这个问题,以便分隔符始终存在?

尝试以下操作:

添加这行

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

在didSelectRowAtIndexPath的开头

这是自iOS7.0起存在的奇怪错误

我知道单元格分隔符消失是一个常见问题,但是当前的解决方案似乎都无法解决我的问题,因此我得出的结论是,当tableView不在编辑模式下时,需要将selectionStyle切换为UITableViewCellSelectionStyleNone ,而在UITableViewCellSelectionStyleBlue时,我处于编辑模式。 我可以通过在包含表视图的视图控制器中调用以下命令来实现此目的:

- (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;
}

然后在我的自定义tableviewcell类中,调用以下命令:

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

}

cellForRowAtIndexPath我留下了更改了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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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