How do I prevent custom UITableViewCells from flashing white on deselecting?

[亡魂溺海] 提交于 2019-12-08 17:22:56

问题


I have a custom UITableViewCell which changes color based on which row it is in:

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

However, when I call deselectRowAtIndexPath:animated:YES, the animation fades to a white color in cells where the selectedBackgroundColor should be darker.

I then realised that the deselection animation has nothing to do with the selectedBackgroundColor; in fact, the deselection animation is actually based on the tableView.backgroundColor property!

How can I override the deselection animation to fade to the background color of my cells?


回答1:


It actually animates back to cell background color so you will have to set it too

Add this line in lighten method

self.backgroundColor = [UIColor whiteColor];

and this in darken method

self.backgroundColor = darkColor;



回答2:


Just set the cell selection to UITableViewCellSelectionStyleNone in the UIBuilder or in code:

cell.selectionStyle = UITableViewCellSelectionStyleNone;


来源:https://stackoverflow.com/questions/10882400/how-do-i-prevent-custom-uitableviewcells-from-flashing-white-on-deselecting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!