使用CALayer设置UITableViewCell背景色 [英] Set UITableViewCell background color using a CALayer

查看:141
本文介绍了使用CALayer设置UITableViewCell背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS上,我正在将CALayer添加到UITableViewCell的图层中。这是我第一次使用CALayer,只是应该更改表格单元格的背景颜色。我的目标是(1)学习如何使用CALayer,以及(2)使用Instruments测试绘图是否比我当前的实现更快,这会降低CGContextFillRect的速度。

On iOS, I am adding a CALayer to a UITableViewCell's layer. This is my first time using CALayer, and it is simply supposed to change the background color of the table cell. My goal is (1) to learn how to use CALayer, and (2) to test using Instruments whether the drawing is faster than my current implementation, which slows down on CGContextFillRect.

Q&A QA1708 是促成所有这些。)

- (void)drawRect:(CGRect)r
{
    UIColor *myColor = [self someColor];
    [myColor set];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextFillRect(context, r);  // draw the background color
    // now draw everything else
    // [...]

}



尝试新实施(无效)



Attempted New Implementation (doesn't work)

#import <QuartzCore/QuartzCore.h>

@implementation MyCell {
    CALayer *backgroundLayer;
}

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        // [...other stuff here too]
        backgroundLayer = [[CALayer alloc] init];
        [[self layer] addSublayer:backgroundLayer];
    }

    return self;
}

- (void)drawRect:(CGRect)r {
    backgroundLayer.frame = CGRectMake(0, 0, r.size.width, r.size.height);
    [backgroundLayer setBackgroundColor:[self someColor]];
    // now draw everything else
    // [...]
}

我看到正确的颜色,但没有其他图形(我假设自定义图形最终出现在我的新图层的 behind 之后)。

I see the correct colors, but none of the other drawing (I'm assuming the custom drawing ends up behind my new layer).

如果我删除了 backgroundLayer.frame = ... 线,则所有其他图形仍然存在,

If I remove the backgroundLayer.frame = ... line, all of my other drawing is still there, but on a black background.

我缺少了什么?

推荐答案

您得到意外行为的原因是由于 UITableViewCell 相对复杂的视图层次结构:

The reason why you're getting unexpected behavior is because of UITableViewCell's relatively complex view hierarchy:

- UITableViewCell
   - contentView
   - backgroundView
   - selectedBackgroundView

每当在 UITableViewCell 中定义自定义绘图例程时,都应该在 contentView 层次结构。这涉及子类化 UIView ,覆盖 -drawRect:并将其作为子视图添加到 contentView

Whenever you define custom drawing routines in a UITableViewCell, you should be doing so within the contentView hierarchy. This involves subclassing UIView, overriding -drawRect:, and adding it as a subview into the contentView.

在您的示例中背景色被忽略的原因是您添加了 CALayer 作为 UITableViewCell 层的子层。这被 UITableViewCell contentView 所掩盖。

The reason why your background color was being ignored in your example was due to your adding your CALayer as a sublayer of the UITableViewCell's layer. This is obscured by the UITableViewCell's contentView.

但是,由于某些原因,您希望在此处使用 CALayer 。我想了解为什么为什么没有 UIView 没有的东西。您可以在 contentView 上设置 backgroundColor ,而不用做这种round回的事情。

However, for some reason, you wish to use a CALayer here. I'd like to understand why as it doesn't have anything that a UIView doesn't have. You can set the backgroundColor on your contentView instead of doing this roundabout set of things.

以下是根据您的要求使用 CALayer 的示例:

Here's an example using CALayer as you requested:

@implementation JRTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if(self) {
      [self addCustomLayerToContentView];
   }
   return self;
}

- (void)addCustomLayerToContentView {
   CALayer *layer = [[CALayer alloc] initWithFrame:[self bounds]];  
   [layer setBackgroundColor:[[UIColor blueColor] cgColor]]; //use whatever color you wish.

   [self.contentView.layer addSublayer:layer];
}

@end

这篇关于使用CALayer设置UITableViewCell背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆