我想自定义UITableView中所有UITableViewCells的背景(以及可能的边框)。到目前为止,我没有成功地完成此项自定义工作,因此我只能使用默认的白色背景单元格。
是否有一种方法可以在iPhone SDK中实现此自定义效果?
我想自定义UITableView中所有UITableViewCells的背景(以及可能的边框)。到目前为止,我没有成功地完成此项自定义工作,因此我只能使用默认的白色背景单元格。
是否有一种方法可以在iPhone SDK中实现此自定义效果?
这是我发现的最有效的解决此问题的方法:使用willDisplayCell代理方法(当使用cell.textLabel.text和/或cell.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]];
这个解决方案在我的情况下非常有效...
您需要将单元格的contentView的backgroundColor设置为您的颜色。如果使用了附件(如披露箭头等),它们将显示为白色,因此您可能需要制作自定义版本。
UITableViewCellAccessoryCheckmark
的链接可以提供?谢谢。 - Dan Rosenstarkcell.textLabel.backgroundColor = [UIColor clearColor];
- Evan Moran这很简单,在iOS 3.0及以上版本,只需在willDisplayCell方法中设置单元格的背景色即可。
不要在cellForRowAtIndexPath方法中设置颜色。
这适用于普通样式和分组样式:
代码:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
P.S:以下为willDisplayCell的文档摘录:
"当表格视图使用单元格绘制一行之前,它会向其委托发送此消息,从而允许委托在显示之前自定义单元格对象。该方法给予委托机会覆盖表格视图先前设置的基于状态的属性,如选中和背景颜色。委托返回后,表格视图仅设置alpha和frame属性,仅在动画行滑入或滑出时设置。"
到目前为止,我所发现的最好方法是设置单元格的背景视图并清除单元格子视图的背景。当然,这只适用于带有索引样式的表格,在具有或不具有附件的情况下都能看起来很好。
这里是一个示例,其中单元格的背景被涂成了黄色:
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提供了一些好的建议 - 最好的方法是创建一个背景视图,为其设置颜色,将其他所有东西都设置为clearColor。此外,我认为这种方式是正确清除颜色的唯一方式(尝试他的示例 - 但将'yellowColor'改为'clearColor'),这也是我试图做的。
UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
[lab setBackgroundColor:[UIColor grayColor]];
cell.backgroundView = lab;
[lab release];
- (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方法中。
cell.backgroundColor = UIColor.clearColor
。 - kbpontius