如何防止自定义 UITableViewCells 在取消选择时闪烁白色



我有一个自定义的UITableViewCell,它会根据它所在的行改变颜色:

TableViewController.m

- (void)willDisplayCell:(GSRSongCell *)cell atIndexPath:(NSIndexPath *)indexPath;
{
    if (indexPath.row % 2 == 0) {
        [cell lighten];
    } else {
        [cell darken];
    }
}

CustomTableViewCell.m

- (void)lighten
{
    self.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.primaryLabel.backgroundColor = [UIColor whiteColor];
    self.secondaryLabel.backgroundColor = [UIColor whiteColor];
}
- (void)darken
{
    UIColor *darkColor = [UIColor colorWithR:241 G:241 B:241 A:1];
    self.selectedBackgroundView.backgroundColor = darkColor;
    self.contentView.backgroundColor = darkColor;
    self.primaryLabel.backgroundColor = darkColor;
    self.secondaryLabel.backgroundColor = darkColor;
}

但是,当我调用deselectRowAtIndexPath:animated:YES时,动画在selectedBackgroundColor应该较暗的单元格中淡化为白色。

然后我意识到取消选择动画与selectedBackgroundColor无关;事实上,取消选择动画实际上是基于tableView.backgroundColor属性的!

如何覆盖取消选择动画以淡入单元格的背景色?

它实际上会动画化回单元格背景颜色,因此您也必须对其进行设置

在减轻方法中添加此行

self.backgroundColor = [UIColor whiteColor];

而这在变暗的方法

self.backgroundColor = darkColor;

只需在UIBuilder或代码中将单元格选择设置为UITableViewCellSelectionStyleNone :

cell.selectionStyle = UITableViewCellSelectionStyleNone;

最新更新