Subclassed UITableViewCell - backgroundView closes everything I do in drawRect

I am trying to create a subclass of UITableViewCell where I draw an image in the upper right corner. I work fine - except when I set self.backgroundView, my background image covers the image drawn in drawRect.

There should be a way to set the background image (and selectedBackgroundView) without closing what is done in drawRect.

Am I going about it wrong?

EDIT: I posted a project with a problem .

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { // TODO: figure out why this covers up self.starImage that 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)]; } 

EDIT 2: on an AWrightIV request, this is how I earned it ... which generally does not require subclassing UITableViewCell. I just add 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; 
+6
iphone uitableview drawrect
source share
4 answers

In fact, you should not mix the drawing with your cell, you are working at a lower level than UITableViewCell, and that is why you are getting this problem.

This is just one of the problems you will encounter. You will encounter other problems as you walk this path, including problems with how choices work.

The right approach is to create a custom UIView that contains the code to draw, and then you can addSubView, which is in your root cell view. This will take care of the rendering in the correct order and will not interfere with the selection system and will work correctly in this case.

+9
source share

You should not override the -drawRect: tables. Instead, create a new custom view and add it to the cellView content item and draw it.

+4
source share

Have you tried adding [super drawRect:rect]; there?

+1
source share

Here's a solution that clings a bit, but it exactly fits my requirements ... with one fatal flaw: when the cells are reused, a star angle appears when I don't want it.

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

I still use drawRect here, but only because self.starImage is null if you access it in the initWithStyle method. In addition, instead of adding a subview to self.contentView, I add it to self.backgroundView to prevent clicking the delete cell button. The angle of the star is positioned correctly in both portrait and landscape modes, and works fine in edit mode.

With the problem of reusing cells, although it's still not ... so maybe I'm trying to do it again without subclassing UITableViewCell.

I am open to any further suggestions. Thanks!

 - (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]; } 
-2
source share

All Articles