Xetius
Xetius

Reputation: 46924

Custom draw a UITableViewCell

I am trying to create a UITableViewCell which overrides the complete drawing of the contents. I have overridden drawRect, which is being called, but it still draws the default contents.

How do I get it to stop drawing the default contents and how do I replace it with my own rendering?

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    DLog (@"Overloaded TableCell initWithStyle");
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    DLog (@"TableCell::drawRect");

    // expecting it to draw nothing
}

Upvotes: 3

Views: 16306

Answers (4)

Patrice Gagnon
Patrice Gagnon

Reputation: 1464

Loren Brichter's blog is no longer available. However, the code was moved here:

https://github.com/enormego/ABTableViewCell

In the hope it's a more permanent URL.

Upvotes: 3

2snacc
2snacc

Reputation: 6353

Loren Brichter (author of Tweetie) talked about this in one of the iTunes U Stanford iPhone Programming course lectures. He said that he had gotten great scrolling performance results by subclassing UITableViewCell and drawing the contents of each cell directly and he gives a code example in his blog post on the subject.

He also notes that apple has added a similar example in one of their code examples.

Upvotes: 9

nicktmro
nicktmro

Reputation: 2288

Have you considered using Interface Builder to create your custom UITableViewCell?

Create your xib and then load it like this:

static NSString *CellIdentifier = @"Cell";
cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
    cell = [nibContents objectAtIndex:0];
}

// do your customization
return cell;

Note that the actual cell is at index 0 in the xib.

Cheers...

Upvotes: 0

Ramin
Ramin

Reputation: 13433

Try creating a subclass of UIView (with your own drawRect) and assign it to the table cell's contentView instead.

Upvotes: 1

Related Questions