子类 UITableViewCell - backgroundView 覆盖了我在 drawRect 中所做的任何事情 [英] subclassed UITableViewCell - backgroundView covers up anything I do in drawRect

查看:17
本文介绍了子类 UITableViewCell - backgroundView 覆盖了我在 drawRect 中所做的任何事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个子类 UITableViewCell,我在右上角绘制图像.我让它完美运行 - 除了当我设置 self.backgroundView 时,我的背景图像覆盖了在 drawRect 中绘制的图像.

I'm trying to make a subclassed UITableViewCell where I draw an image in the upper right corner. I have it working perfectly - except when I set self.backgroundView, my background image covers up the image drawn in drawRect.

必须有一种方法可以设置背景图像(和 selectedBackgroundView)而不覆盖 drawRect 中正在执行的操作.

There must be a way to be able to set a background image (and the selectedBackgroundView) without covering up what's being done in drawRect.

我是不是用错了方法?

我发布了一个存在问题的示例项目.

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {

     // TODO: figure out why this covers up self.starImage that's drawn in drawRect
         self.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBackground.png"]] autorelease];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [self.starImage drawAtPoint:CGPointMake(self.bounds.size.width - self.starImage.size.width, 0.0)];
}

<小时>

编辑 2:在 AWrightIV 的要求下,这就是我如何让它工作......根本不需要子类化 UITableViewCell.我只是向 cell.backgroundView 添加一个子视图:


EDIT 2: at AWrightIV's request, here's how I got it working... which didn't require subclassing UITableViewCell at all. I'm just adding a subview to cell.backgroundView:

// create a UIImageView that contains the background image for the cell
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBackground.png"]];

// create another UIImageView that contains the corner image
UIImage *starRedImage = [UIImage imageNamed:@"starcorner_red.png"];
UIImageView *starImageView = [[UIImageView alloc] initWithFrame:CGRectMake(297,
                                                                               0,
                                                                               starRedImage.size.width,
                                                                               starRedImage.size.height)];
starImageView.image = starRedImage;

// add the corner UIImageView as a subview to the background UIImageView
[bgImageView addSubview:starImageView];

// set cell.background to use the background UIImageView
cell.backgroundView = bgImageView;

推荐答案

你真的不应该像那样将绘图和你的单元格混合在一起,你在比 UITableViewCell 机器运行的更低级别上操作,这是为什么会出现这个问题.

You are not really supposed to mix the drawing with your cell like that, you are operating at a lower-level than the UITableViewCell machinery is operating, and this is why you get this problem.

这只是您最终会遇到的各种问题之一.在沿着这条路走的过程中,您会遇到其他问题,包括选择工作方式的问题.

This is just one of the various problems you will end up running into. You will run into other problems as you go down that path, including problems with how the selection works.

正确的方法是创建一个包含要绘制的代码的自定义 UIView,然后您可以将其添加到单元格的根视图中.这将按照正确的顺序处理渲染,并且不会干扰选择系统,并且在这种情况下可以正常工作.

The proper approach is to create a custom UIView that contains the code to draw, and then you can addSubView that into your cell's root view. That will take care of the rendering in the proper order, and wont interfere with the selection system, and will work correctly in this case.

这篇关于子类 UITableViewCell - backgroundView 覆盖了我在 drawRect 中所做的任何事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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