【发布时间】:2010-09-21 20:15:34
【问题描述】:
我想自定义 UITableView 中所有 UITableViewCells 的背景(也可能还有边框)。到目前为止,我还不能自定义这些东西,所以我有一堆默认的白色背景单元格。
有没有办法用 iPhone SDK 做到这一点?
【问题讨论】:
标签: ios iphone uitableview cocoa-touch background-color
我想自定义 UITableView 中所有 UITableViewCells 的背景(也可能还有边框)。到目前为止,我还不能自定义这些东西,所以我有一堆默认的白色背景单元格。
有没有办法用 iPhone SDK 做到这一点?
【问题讨论】:
标签: ios iphone uitableview cocoa-touch background-color
这是我遇到的解决此问题的最有效方法,使用 willDisplayCell 委托方法(这在使用 cell.textLabel.text 和/或单元格时也会处理文本标签背景的白色。 detailTextLabel.text):
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }
当调用此委托方法时,单元格的颜色是通过单元格而不是表格视图控制的,就像您使用时一样:
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... }
因此,在单元格委托方法的主体中,添加以下代码以交替使用单元格颜色,或者仅使用函数调用使表格的所有单元格具有相同的颜色。
if (indexPath.row % 2)
{
[cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
}
else [cell setBackgroundColor:[UIColor clearColor]];
这个解决方案在我的情况下效果很好......
【讨论】:
cell.backgroundColor = UIColor.clearColor
您需要将单元格的 contentView 的 backgroundColor 设置为您的颜色。如果您使用配件(例如披露箭头等),它们将显示为白色,因此您可能需要滚动这些配件的自定义版本。
【讨论】:
UITableViewCellAccessoryCheckmark 的任何链接?谢谢。
cell.textLabel.backgroundColor = [UIColor clearColor];
这真的很简单,因为 OS 3.0 只是在 willDisplayCell 方法中设置单元格的背景颜色。 不得在 cellForRowAtIndexPath 中设置颜色。
这适用于普通样式和分组样式:
代码:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
P.S:这里是 willDisplayCell 的文档摘录:
"表格视图将此消息发送到 它的代表就在它使用单元之前 画一排,从而允许 委托自定义单元格对象 在显示之前。这种方法 让代表有机会 覆盖基于状态的属性集 更早的由表视图,如 选择和背景颜色。后 委托返回,表格视图 仅设置 alpha 和 frame 属性,然后仅当 当它们滑入时动画行或 出去。”
我在this post from colionel 中找到了此信息。谢谢他!
【讨论】:
到目前为止,我发现的最佳方法是设置单元格的背景视图并清除单元格子视图的背景。当然,这在只有索引样式的表格上看起来不错,无论是否有配件。
这是一个单元格背景为黄色的示例:
UIView *backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ];
backgroundView.backgroundColor = [ UIColor yellowColor ];
cell.backgroundView = backgroundView;
for ( UIView *view in cell.contentView.subviews )
{
view.backgroundColor = [ UIColor clearColor ];
}
【讨论】:
只需将它放在你的 UITableView 委托类文件 (.m) 中:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIColor *color = ((indexPath.row % 2) == 0) ? [UIColor colorWithRed:255.0/255 green:255.0/255 blue:145.0/255 alpha:1] : [UIColor clearColor];
cell.backgroundColor = color;
}
【讨论】:
我同意 Seba, 我尝试在 rowForIndexPath 委托方法中设置交替行颜色,但在 3.2 和 4.2 之间得到不一致的结果。以下内容对我很有用。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ((indexPath.row % 2) == 1) {
cell.backgroundColor = UIColorFromRGB(0xEDEDED);
cell.textLabel.backgroundColor = UIColorFromRGB(0xEDEDED);
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
else
{
cell.backgroundColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
}
【讨论】:
在尝试了所有不同的解决方案之后,以下方法是最优雅的方法。 在以下委托方法中更改颜色:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (...){
cell.backgroundColor = [UIColor blueColor];
} else {
cell.backgroundColor = [UIColor whiteColor];
}
}
【讨论】:
vlado.grigorov 有一些很好的建议 - 最好的方法是创建一个 backgroundView,并给它一个颜色,将其他所有内容设置为 clearColor。此外,我认为这种方式是正确清除颜色的唯一方法(尝试他的示例 - 但设置 'clearColor' 而不是 'yellowColor'),这就是我想要做的。
【讨论】:
自定义表格视图单元格的背景最终成为“全有或全无”的方法。很难以一种看起来不奇怪的方式更改用于内容单元格背景的颜色或图像。
原因是单元格实际上跨越了视图的宽度。圆角只是其绘图风格的一部分,内容视图位于此区域。
如果您更改内容单元格的颜色,您最终会在角落看到白色位。如果您更改整个单元格的颜色,您将拥有一个横跨视图宽度的颜色块。
您当然可以自定义一个单元格,但它并不像您最初想象的那么简单。
【讨论】:
创建一个视图并将其设置为单元格的背景视图
UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
[lab setBackgroundColor:[UIColor grayColor]];
cell.backgroundView = lab;
[lab release];
【讨论】:
扩展 N.Berendt 的回答 - 如果您想根据实际单元格数据对象中的某些状态设置单元格颜色,则在您配置表格单元格的其余信息时,通常是当您在 cellForRowAtIndexPath 方法中,您可以通过覆盖 willDisplayCell 方法以从内容视图背景颜色设置单元格背景颜色来做到这一点 - 这解决了披露按钮背景等问题不正确但仍然允许您控制颜色cellForRowAtIndexPath 方法,您在其中进行所有其他单元格自定义。
所以: 如果将此方法添加到表视图委托中:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = cell.contentView.backgroundColor;
}
然后在你的 cellForRowAtIndexPath 方法中你可以这样做:
if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
cell.contentView.backgroundColor = [UIColor blueColor];
}
这省去了在 willDisplayCell 方法中再次检索数据对象的麻烦,还省去了在两个地方对表格单元格进行剪裁/自定义 - 所有自定义都可以在 cellForRowAtIndexPath 方法中进行。
【讨论】:
使用 Photoshop 或 gimp 创建一个图像以用作背景,并将其命名为 myimage。 然后,将此方法添加到您的 tableViewController 类中:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *cellImage = [UIImage imageNamed:@"myimage.png"];//myimage is a 20x50 px with gradient color created with gimp
UIImageView *cellView = [[UIImageView alloc] initWithImage:cellImage];
cellView.contentMode = UIContentViewModeScaleToFill;
cell.backgroundView = cellView;
//set the background label to clear
cell.titleLabel.backgroundColor= [UIColor clearColor];
}
如果您在属性检查器中将 UITableView 设置为自定义,这也将起作用。
【讨论】:
我的解决方案是在 cellForRowAtIndexPath 事件中添加以下代码:
UIView *solidColor = [cell viewWithTag:100];
if (!solidColor) {
solidColor = [[UIView alloc] initWithFrame:cell.bounds];
solidColor.tag = 100; //Big tag to access the view afterwards
[cell addSubview:solidColor];
[cell sendSubviewToBack:solidColor];
[solidColor release];
}
solidColor.backgroundColor = [UIColor colorWithRed:254.0/255.0
green:233.0/255.0
blue:233.0/255.0
alpha:1.0];
在任何情况下都可以工作,即使使用披露按钮也是如此,并且我认为在 cellForRowAtIndexPath 中对单元格颜色状态进行操作比在 cellWillShow 事件中更适合您的逻辑。
【讨论】:
继承 UITableViewCell 并将其添加到实现中:
-(void)layoutSubviews
{
[super layoutSubviews];
self.backgroundColor = [UIColor blueColor];
}
【讨论】:
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1];
cell.backgroundView = bg;
[bg release];
【讨论】:
这将适用于最新的 Xcode。
-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
cell.backgroundColor = [UIColor grayColor];
}
【讨论】:
试试这个:
- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
}
【讨论】: