6

我正在尝试制作一个子类 UITableViewCell 我在右上角绘制图像。我让它工作得很好——除了我设置 self.backgroundView 时,我的背景图像覆盖了在 drawRect 中绘制的图像。

必须有一种方法可以设置背景图像(和 selectedBackgroundView),而不会掩盖在 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 中添加一个子视图:

// 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;
4

4 回答 4

9

您不应该像那样将绘图与您的单元格混合,您的操作级别低于 UITableViewCell 机器的操作级别,这就是您遇到此问题的原因。

这只是您最终会遇到的各种问题之一。当你沿着这条路走下去时,你会遇到其他问题,包括选择如何工作的问题。

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

于 2010-08-20T04:06:21.957 回答
4

您不应该覆盖表格单元的 -drawRect: 。相反,创建一个新的自定义视图并将其添加到单元格的 contentView,并在那里绘制。

于 2010-08-20T04:00:46.317 回答
1

您是否尝试过在[super drawRect:rect];那里添加?

于 2010-08-20T03:23:45.430 回答
-2

Here's a solution that's a bit of a kludge, but it fits my requirements exactly... with one fatal flaw: when cells get reused, the star corner shows up when I don't want it to.

http://dl.dropbox.com/u/2349787/UIImage_Position_subclassed_cell2.zip

I'm still using drawRect here, but only because self.starImage is null if you access it within the initWithStyle method. Also, instead of adding the subview to self.contentView, I'm adding it to self.backgroundView to prevent the cell's delete button from interfering with it. The star corner is positioned correctly in both portrait and landscape mode, and works fine within edit mode as well.

With the cell reuse issue though, It's still a no go... so, maybe I'm back to trying to do it without subclassing UITableViewCell.

I'm open to any further suggestions. Thank you!

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code

        self.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellBackground.png"]] autorelease];
    }
    return self;
}

- (void) drawRect:(CGRect)rect {

    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width - self.starImage.size.width, 0, self.starImage.size.width, self.starImage.size.height)] autorelease];
    imageView.image = self.starImage;
    imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

    [self.backgroundView addSubview:imageView];
}
于 2010-08-20T12:36:32.817 回答