2011-06-14 92 views
3

我的子类的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]时,结果不太好看。

Wrong result

我怎样才能达到良好的效果,且不欠缺细胞分离机线?

回答

8

最后,我自己解决了。我用下面的代码- (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]; 
0

为什么不在设置新的单元格颜色之前,捕获UIColor变量中的旧单元格颜色。 尝试 UIColor * originalColor = anObject.backgroundColor; 然后当您想要更改为只更改单元格颜色为originalColor时。

+0

它不工作 – edo42 2011-06-14 16:49:18

+0

它有什么作用?根本改变颜色? – James 2011-06-14 16:57:22

+0

我有一些内存问题,但我修好了,它返回灰色 – edo42 2011-06-14 17:00:04

10

我有一个类似的问题,发现edo42的答案。然而,当时我的单元格背后的背景背景出现问题,没有显示我设置的背景颜色。我相信这是由于风格:UITableViewCellStyleSubtitle。

在其他情况下,偶然发现了这个问题,我相信这样做的更好的解决方案在这一问题中发现:

UITableViewCellStyleSubtitle的BACKGROUNDCOLOR标签? BackgroundColor of UITableViewCellStyleSubtitle labels?

答案在这里转载:

要更改表格视图单元格的背景颜色,你需要设置它的tableView:willDisplayCell:forRowAtIndexPath:不是的tableView:的cellForRowAtIndexPath:否则将无法有任何影响,例如:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell.backgroundColor = [UIColor whiteColor]; 
} 
0

我觉得最好从自定义单元格的.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的颜色,因为你的细胞往往不会及时调整快速选曲用户。

0

你应该总是提供willDisplayCell细胞的变化:forRowAtIndex代替cellForRowAtIndex方法,按照苹果的文档

这个作品!

0

改变颜色下面的委托方法:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (...){ 
     cell.backgroundColor = [UIColor blueColor]; 
    } else { 
     cell.backgroundColor = [UIColor whiteColor]; 
    } 
}