Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何自定义UITableViewCell的背景色?_Ios_Iphone_Uitableview_Cocoa Touch_Background Color - Fatal编程技术网

Ios 如何自定义UITableViewCell的背景色?

Ios 如何自定义UITableViewCell的背景色?,ios,iphone,uitableview,cocoa-touch,background-color,Ios,Iphone,Uitableview,Cocoa Touch,Background Color,我想自定义UITableView中所有UITableViewCell的背景(可能还有边框)。到目前为止,我还不能自定义这个东西,所以我有一堆白色的背景单元格,这是默认的 iPhone SDK有没有办法做到这一点?您需要将手机内容视图的背景颜色设置为您的颜色。如果您使用附件(如显示箭头等),它们将显示为白色,因此您可能需要滚动这些附件的自定义版本。自定义表格视图单元格的背景最终会变成“全部或无”的方式。以一种看起来不奇怪的方式更改内容单元格背景的颜色或图像是非常困难的 原因是该单元实际上跨越了视图

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


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

您需要将手机内容视图的背景颜色设置为您的颜色。如果您使用附件(如显示箭头等),它们将显示为白色,因此您可能需要滚动这些附件的自定义版本。

自定义表格视图单元格的背景最终会变成“全部或无”的方式。以一种看起来不奇怪的方式更改内容单元格背景的颜色或图像是非常困难的

原因是该单元实际上跨越了视图的宽度。圆角只是其绘图样式的一部分,内容视图位于该区域

如果您更改内容单元格的颜色,您将在角落处看到白色位。如果更改整个单元的颜色,则将有一块横跨视图宽度的颜色


您当然可以自定义单元格,但这并不像您一开始想象的那么简单。

到目前为止,我找到的最佳方法是设置单元格的背景视图和清除单元格子视图的背景。当然,无论有无附件,这在仅具有索引样式的桌子上都很好看

下面是单元格背景为黄色的示例:

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 ];
}

grigorov有一些很好的建议-最好的方法是创建一个背景视图,并给它一个颜色,将所有其他设置为clearColor。此外,我认为这种方法是正确清除颜色的唯一方法(尝试他的示例-但设置“clearColor”而不是“yellowColor”),这就是我试图做的。

这是我遇到的解决此问题的最有效方法,使用willDisplayCell委托方法(在使用cell.textlab.text和/或cell.detailtextlab.text时,也会考虑文本标签背景的白色):

调用此委托方法时,单元格的颜色通过单元格而不是表格视图进行控制,如使用:

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... }
因此,在cell delegate方法的主体中,添加以下代码以替换单元格的颜色,或者使用函数调用使表中的所有单元格颜色相同

if (indexPath.row % 2)
{
    [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
}
else [cell setBackgroundColor:[UIColor clearColor]];

此解决方案在我的情况下运行良好…

只需将其放入UITableView委托类文件(.m):


这非常简单,因为OS3.0只需在willDisplayCell方法中设置单元格的背景色。 不能在cellForRowAtIndexPath中设置颜色

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

代码:

备注:这里是willDisplayCell的文档摘录:

“表视图将此消息发送到 它的委托就在它使用单元格之前 划一行,从而允许 委托以自定义单元格对象 在显示之前。此方法 让学员有机会 重写基于状态的属性集 表视图的前面部分,例如 选择和背景色 委托返回表视图 仅设置alpha和帧 属性,然后仅当 在行滑入或滑入时设置行动画 出去。”

我在中找到了这个信息。谢谢他!

我同意塞巴的观点, 我尝试在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;
    }

}

我的解决方案是向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事件中更好。我认为。

创建一个视图并将其设置为单元格的背景视图

UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
            [lab setBackgroundColor:[UIColor grayColor]];
            cell.backgroundView = lab;
            [lab release];

扩展N.Berendt的答案-如果要根据实际单元格数据对象中的某些状态设置单元格颜色,在配置表格单元格的其余信息时(通常是在CellForRowatineXpath方法中),可以通过重写willDisplayCell方法来设置单元格背景内容视图背景色的nd颜色-这绕过了公开按钮背景等不正确的问题,但仍然允许您在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方法中进行。

子类UITableViewCell并将其添加到实现中:

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.backgroundColor = [UIColor blueColor];
}

创建一个图像用作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设置为custom(自定义),这也会起作用。

在尝试了所有不同的解决方案后,以下方法是最优雅的方法。
    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];
在以下委托方法中更改颜色:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (...){
        cell.backgroundColor = [UIColor blueColor];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
    }
}
试试这个:

- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
    tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
}

这将在最新的Xcode中工作

-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
    cell.backgroundColor = [UIColor grayColor];
}

分组表中的单元格确实如此,但是普通表没有那么多需要担心的。没有附件的简单单元格应该看起来不错。Ben-好的观点。我主要使用分组表,但正如你提到的,普通表没有任何问题。如果这样做,请确保将不透明属性也设置为no。U真的不推荐使用透明单元格控件。滚动性能会受到很大影响,这就是为什么苹果总是说使用不透明单元格控件
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (...){
        cell.backgroundColor = [UIColor blueColor];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
    }
}
- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
    tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
}
-(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {
    cell.backgroundColor = [UIColor grayColor];
}