Cocoa-Touch: How to: custom UITableViewCell with UIColor clearColor background
I have a UITableView
with custom cells.
MyTableViewCell : UITableViewCell
and MyTableViewCellContentView : UIView
classes.
What I'm doing is basically what is done in the AdvancedTableViewCells demo app from Apple with a slight change, on some rows I want to use a clearColor background to show the table's background behind the painted text.
So in MyTableView
's cellForRowAtIndexPath
I'm doing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MyCell";
MyTableViewCell *cell = (MyTableViewCell *)[tabl开发者_JAVA百科eView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.someValue = indexPath.section;
[cell finishedSetup];
return cell;
}
And in my MyTableViewCell
's finishedSetup
:
cellContentView = [[MyTableViewCellContentView alloc] initWithFrame:CGRectMake(0, 0, 320, 80) cell:self];
cellContentView.autoresizingMask = UIViewAutoresizingNone;
cellContentView.contentMode = UIViewContentModeRedraw;
[self.contentView addSubview:cellContentView];
And in MyTableViewCellContentView
I implement the drawRect
method. And plan to not use any subviews but draw my custom content just as the Apple example does in the CompositeSubviewBasedApplication
.
The problem is that for a few sections I want to use a clearColor
backgroundColor
. This works, until a cell with a non-clearColor
backgroundColor
is reused to draw a clearColor
cell, at which time the background is not cleared and will still have the old color.
How can I make the background redraw?
Edit:
I forgot to mention, I'm setting the background color in MyTableViewCellContentView
's init after calling super's init. Setting it via:
self.backgroundColor = [UIColor clearColor];
I've verified that this in fact does get called and is called as expected with clearColor or redColor.
I've also tried setting the table cell's background color, it didn't help.
Edit #2: Here's my drawRect method:
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
static int i = 0;
[[NSString stringWithFormat:@"Cell %d", ++i] drawAtPoint:CGPointMake(3, 3) withFont:[UIFont boldSystemFontOfSize:16]];
}
To make the background color setting take effect you need to do the setting in tableView:willDisplayCell:forRowAtIndexPath
- the OS will not alter anything you set here. The reason is that some additional setup of the cell gets done by the OS after you return it from tableView:cellForRowAtIndexPath
. If you can, get a look at session 101 from WWDC 09 presented by Jason Beaver.
I haven't found out why it was happening. But I've tried a different strategy. I'm no longer reusing table cells, since I don't have that many of them.
Instead I'm creating all cells at startup and actually get a boost in performance when showing them since this way there is no additional setup needed when scrolling.
精彩评论