edo*_*o42 4 cocoa-touch objective-c uitableview
我将UITableViewCell子类化为将单元格背景颜色设置为我需要的颜色:
.H
@interface DataViewCustomCell : UITableViewCell {
UIColor* cellColor;
UIColor* standardColor;
}
- (void) setCellColor: (UIColor*)color;
@end
Run Code Online (Sandbox Code Playgroud)
.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
Run Code Online (Sandbox Code Playgroud)
当我用我想要的颜色调用setCellColor时,一切顺利,但是当我找不到恢复原始颜色的方法时:当我[UIColor clearColor]使用UITableViewStylePlain样式设置时,结果看起来并不好看.
如何在不丢失细胞分离株系的情况下获得良好的结果?
Jos*_*lfe 10
我遇到了类似的问题,发现了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];
}
Run Code Online (Sandbox Code Playgroud)
最后,我自己解决了.我使用以下代码- (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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4781 次 |
| 最近记录: |