2012-06-04 100 views
9

我有一个自定义的UITableViewCell基于它是在哪一行而改变颜色:如何防止自定义UITableViewCells在取消选择时闪烁白色?

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属性!

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

+0

我也尝试将tableView.backgroundColor设置为[UIColor clearColor] - 这不起作用。 – oliland

回答

9

它实际上以动画方式返回单元格背景颜色,所以你必须将它设置得

在减仓法

self.backgroundColor = [UIColor whiteColor]; 

加入这一行,这在变暗方法

self.backgroundColor = darkColor; 
+1

不敢相信我没有尝试过。谢谢! – oliland

3

刚将单元格选择设置为UIBuilder中或代码中的UITableViewCellSelectionStyleNone:

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
+0

对于我在Swift中有'cell.selectionStyle = UITableViewCellSelectionStyle.None' –

相关问题