我正在尝试制作一个子类 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;