ios 如何自定义 UITableViewCell 的背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/281515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to customize the background color of a UITableViewCell?
提问by jpm
I would like to customize the background (and maybe the border too) of all of the UITableViewCells within my UITableView. So far I have not been able to customize this stuff, so I have a bunch of white background cells which is the default.
我想自定义 UITableView 中所有 UITableViewCells 的背景(也可能是边框)。到目前为止,我还不能自定义这些东西,所以我有一堆默认的白色背景单元格。
Is there a way to do this with the iPhone SDK?
有没有办法用 iPhone SDK 做到这一点?
回答by Nathan B.
Here is the most efficient way I have come across to solve this problem, use the willDisplayCell delegate method (this takes care of the white color for the text label background as well when using cell.textLabel.text and/or cell.detailTextLabel.text):
这是我遇到的解决此问题的最有效方法,使用 willDisplayCell 委托方法(这在使用 cell.textLabel.text 和/或 cell.detailTextLabel.text 时也处理文本标签背景的白色):
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }
When this delegate method is called the color of the cell is controlled via the cell rather than the table view, as when you use:
当这个委托方法被调用时,单元格的颜色是通过单元格而不是表格视图来控制的,当你使用时:
- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... }
So within the body of the cell delegate method add the following code to alternate colors of cells or just use the function call to make all the cells of the table the same color.
因此,在单元格委托方法的主体中,将以下代码添加到单元格的交替颜色中,或者仅使用函数调用使表格的所有单元格具有相同的颜色。
if (indexPath.row % 2)
{
[cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
}
else [cell setBackgroundColor:[UIColor clearColor]];
This solution worked well in my circumstance...
这个解决方案在我的情况下效果很好......
回答by Ben Gottlieb
You need to set the backgroundColor of the cell's contentView to your color. If you use accessories (such as disclosure arrows, etc), they'll show up as white, so you may need to roll custom versions of those.
您需要将单元格 contentView 的 backgroundColor 设置为您的颜色。如果您使用配件(例如披露箭头等),它们将显示为白色,因此您可能需要滚动这些配件的自定义版本。
回答by Seba
This is really simple, since OS 3.0 just set the background color of the cell in the willDisplayCell method. You must not set the color in the cellForRowAtIndexPath.
这真的很简单,因为 OS 3.0 只是在 willDisplayCell 方法中设置单元格的背景颜色。您不得在 cellForRowAtIndexPath 中设置颜色。
This works for both the plain and grouped style :
这适用于普通样式和分组样式:
Code:
代码:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
P.S: Here the documentation extract for willDisplayCell :
PS:这里是 willDisplayCell 的文档摘录:
"A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out."
“表格视图在使用单元格绘制行之前将此消息发送给其委托,从而允许委托在显示之前自定义单元格对象。此方法使委托有机会覆盖先前设置的基于状态的属性表格视图,例如选择和背景颜色。委托返回后,表格视图仅设置 alpha 和 frame 属性,然后仅在行滑入或滑出时设置动画。”
I've found this information in this post from colionel. Thank him!
我在colionel 的这篇文章中找到了这些信息。谢谢他!
回答by Vladimir Grigorov
The best approach I've found so far is to set a background view of the cell and clear background of cell subviews. Of course, this looks nice on tables with indexed style only, no matter with or without accessories.
到目前为止,我发现的最好方法是设置单元格的背景视图和单元格子视图的清晰背景。当然,这在只有索引样式的桌子上看起来不错,无论有没有配件。
Here is a sample where cell's background is panted yellow:
这是一个示例,其中单元格的背景为黄色:
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 ];
}
回答by JAG
Simply put this in you UITableView delegate class file (.m):
简单地把它放在你的 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;
}
回答by cipherz
I concur with Seba, I tried to set my alternating row color in the rowForIndexPath delegate method but was getting inconsistent results between 3.2 and 4.2. The following worked great for me.
我同意 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;
}
}
回答by Jim Huang
After trying out all different solutions, the following method is the most elegant one. Change the color in the following delegate method:
在尝试了所有不同的解决方案后,以下方法是最优雅的一种。在以下委托方法中更改颜色:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (...){
cell.backgroundColor = [UIColor blueColor];
} else {
cell.backgroundColor = [UIColor whiteColor];
}
}
回答by William Denniss
vlado.grigorov has some good advice - the best way is to create a backgroundView, and give that a colour, setting everything else to the clearColor. Also, I think that way is the only way to correctly clear the colour (try his sample - but set 'clearColor' instead of 'yellowColor'), which is what I was trying to do.
vlado.grigorov 有一些很好的建议 - 最好的方法是创建一个 backgroundView,并给它一个颜色,将其他所有内容设置为 clearColor。此外,我认为这种方式是正确清除颜色的唯一方法(尝试他的示例 - 但设置“clearColor”而不是“yellowColor”),这就是我想要做的。
回答by Andrew Grant
Customizing the background of a table view cell eventually becomes and "all or nothing" approach. It's very difficult to change the color or image used for the background of a content cell in a way that doesn't look strange.
自定义表格视图单元格的背景最终成为“全有或全无”的方法。以一种看起来并不奇怪的方式更改用于内容单元格背景的颜色或图像是非常困难的。
The reason is that the cell actually spans the width of the view. The rounded corners are just part of its drawing style and the content view sits in this area.
原因是单元格实际上跨越了视图的宽度。圆角只是其绘图风格的一部分,内容视图位于该区域。
If you change the color of the content cell you will end up with white bits visible at the corners. If you change the color of the entire cell, you will have a block of color spanning the width of the view.
如果您更改内容单元格的颜色,您最终会在角落处看到白色部分。如果您更改整个单元格的颜色,您将拥有一个跨越视图宽度的颜色块。
You can definitely customize a cell, but it's not quite as easy as you may think at first.
您绝对可以自定义单元格,但它并不像您最初想象的那么容易。
回答by Senthil
Create a view and set this as background view of the cell
创建一个视图并将其设置为单元格的背景视图
UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
[lab setBackgroundColor:[UIColor grayColor]];
cell.backgroundView = lab;
[lab release];