UITableViewCell背景色问题

问题描述

| 我将UITableViewCell子类化为将单元格背景色设置为我需要的颜色: 。H
@interface DataViewCustomCell : UITableViewCell {
    UIColor* cellColor;
    UIColor* standardColor;
}
- (void) setCellColor: (UIColor*)color;

@end
.m
@implementation DataViewCustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(Nsstring *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void) setCellColor: (UIColor*)color
{
    cellColor = color;
}

- (void) spreadBackgroundColor: (UIView*)that withColor: (UIColor*)bkColor
{
    NSEnumerator *enumerator = [that.subviews objectEnumerator];
    id anObject;

    while (anObject = [enumerator nextObject]) {
        if([anObject isKindOfClass: [UIView class]])
        {
            ((UIView*)anObject).backgroundColor = bkColor;
            [self spreadBackgroundColor:anObject withColor:bkColor];
        }
    }
}

- (void) layoutSubviews {
    [super layoutSubviews]; // layouts the cell as UITableViewCellStyleValue2 would normally look like

    if(!self.selected && NULL != cellColor)
    {
        [self spreadBackgroundColor:self withColor:cellColor];
    }
}

- (void)dealloc
{
    [super dealloc];
}

@end
当我用所需的颜色调用setCellColor时,一切顺利,但是当我还没有找到将原始颜色设置回去的方法时:当我用UITableViewStylePlain样式设置
[UIColor clearColor]
时,结果并不好看。   如何在不丢失单元格分隔线的情况下取得良好的效果?     

解决方法

        我遇到了类似的问题,找到了edo42的答案。但是,那时单元格中文本后面的背景没有显示我设置的背景色,因此我遇到了问题。我相信这是由于样式:UITableViewCellStyleSubtitle。 如果其他人偶然发现了这个问题,我相信可以在这个问题中找到更好的解决方案: UITableViewCellStyleSubtitle标签的BackgroundColor? UITableViewCellStyleSubtitle标签的BackgroundColor? 答案转载于此: 要更改表格视图单元格的背景颜色,您需要在tableView:willDisplayCell:forRowAtIndexPath:中设置它,而不是在tableView:cellForRowAtIndexPath:中进行设置,否则它将没有任何效果,例如:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor whiteColor];
}
    ,        最后,我自己解决了。我在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中使用以下代码而不是子类化:
    UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
    bg.backgroundColor = [UIColor greenColor]; //The color you want
    cell.backgroundView = bg;
    cell.textLabel.backgroundColor = bg.backgroundColor;
    [bg release];
    ,        为什么不设置新的单元格颜色之前,请在UIColor变量中捕获旧的单元格颜色。 尝试 UIColor * originalColor = anObject.backgroundColor; 然后,当您想更改回原来的颜色时,只需将单元格颜色更改为originalColor。     ,        我发现最好从自定义单元的.m文件而不是tableViewController中调整颜色。似乎更直观,并且稍微容易一些。只需根据需要在方法中调整颜色:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
注意:请确保在调用结束时也调用这些方法的超级方法,例如如果您想使单元格变为绿色:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
     self.backgroundColor = [UIColor greenColor];
     [super touchesBegan:touches withEvent:event];
}
另外,根据我的经验,不应该在touchesEnded中调整颜色,因为您的单元格通常不会及时调整以供用户快速选择。     ,        根据Apple文档,您应该始终在willDisplayCell:forRowAtIndex中提供对单元格的更改,而不是cellForRowAtIndex方法。 这有效!     ,        在以下委托方法中更改颜色:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (...){
        cell.backgroundColor = [UIColor blueColor];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
    }
}