Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subclassed UITableViewCell - backgroundView covers up anything I do in 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.

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.

Am I going about this the wrong way?

EDIT: I've posted an example project with the 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'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)];
}

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;
like image 686
Jim Rhoades Avatar asked Aug 20 '10 03:08

Jim Rhoades


3 Answers

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.

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.

like image 197
miguel.de.icaza Avatar answered Nov 14 '22 23:11

miguel.de.icaza


You shouldn't override the -drawRect: of a tablecell. Instead, create a new custom view and add it to the cell's contentView, and draw in there.

like image 33
Ben Gottlieb Avatar answered Nov 14 '22 22:11

Ben Gottlieb


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

like image 32
Stefan Arentz Avatar answered Nov 14 '22 22:11

Stefan Arentz