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

oli*_*and 9 cocoa-touch core-animation objective-c uikit ios

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

TableViewController.m

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

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;
}
Run Code Online (Sandbox Code Playgroud)

然而,当我打电话时deselectRowAtIndexPath:animated:YES,动画会在细胞中逐渐消失为白色selectedBackgroundColor.

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

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

Yog*_*ari 9

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

在lighten方法中添加此行

self.backgroundColor = [UIColor whiteColor];
Run Code Online (Sandbox Code Playgroud)

这是在变暗的方法

self.backgroundColor = darkColor;
Run Code Online (Sandbox Code Playgroud)


Geo*_*rge 5

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

cell.selectionStyle = UITableViewCellSelectionStyleNone;
Run Code Online (Sandbox Code Playgroud)