3

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
}
Xetius
  • 44,755
  • 24
  • 88
  • 123

4 Answers4

9

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.

prairiedogg
  • 6,323
  • 8
  • 44
  • 52
  • this is also always covered a ton at WWDC, if you can get the session videos. – Michael Grinich Jun 21 '10 at 10:25
  • 4
    I don't see how this response answer's the question: 'I have overridden drawRect, which is being called, but it still draws the default contents.' – tyler Oct 30 '12 at 18:18
3

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.

Patrice Gagnon
  • 1,276
  • 14
  • 14
  • There is a snapshot of the Blog entry in the web.archive: http://web.archive.org/web/20081215081040/http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/ – Olaf Jan 01 '14 at 11:01
1

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

Ramin
  • 13,343
  • 3
  • 33
  • 35
0

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...

nicktmro
  • 2,298
  • 2
  • 22
  • 31