我想自定义UITableView中所有UITableViewCell的背景(也可能是边框)。到目前为止,我还没有能够自定义这些东西,所以我有一堆白色背景单元格,这是默认的。

有没有办法用iPhone SDK做到这一点?

有帮助吗?

解决方案

您需要将单元格contentView的backgroundColor设置为您的颜色。如果您使用配件(例如公开箭头等),它们将显示为白色,因此您可能需要滚动它们的自定义版本。

其他提示

这是我遇到解决此问题的最有效方法,使用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]];

此解决方案在我的情况下运作良好......

这非常简单,因为OS 3.0只是在willDisplayCell方法中设置单元格的背景颜色。 您不能在cellForRowAtIndexPath中设置颜色。

这适用于普通和分组样式:

代码:

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

P.S:这里是willDisplayCell的文档摘录:

  

"表视图将此消息发送到   它的代表就在它使用单元格之前   画一排,从而允许   委托自定义单元格对象   在它显示之前。这种方法   给代表一个机会   覆盖基于状态的属性集   早先由表视图,如   选择和背景颜色。后   委托返回,表视图   仅设置alpha和框架   属性,然后只有当   在幻灯片中插入动画或动画   。出来"

我在这个中找到了这些信息。来自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;
}

我同意塞巴,    我尝试在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"]];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top