4

Hi I am trying to draw strings in my UITableViewCell in iOS 7 with the following code

-(void)drawRect:(CGRect)rect{
[super drawRect:rect];
CGRect playerNameRect = CGRectMake(0, kCellY, kPlayerNameSpace, kCellHeight);

NSDictionary*dictonary = [NSDictionary
                          dictionaryWithObjectsAndKeys:
                          [UIColor hmDarkGreyColor], NSForegroundColorAttributeName,
                          kFont, NSFontAttributeName,
                          nil];

[self.playerName drawInRect:playerNameRect withAttributes:dictonary];

}

However I can not get anything to appear... self.playerName is not nil, and the playerNameRect is correct.

I was previously using the following code to do the same thing but was recently deprecated in iOS 7

        [self.playerName drawInRect:playerNameRect withFont:kFont lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentCenter];

What is also strange is I can not get anything to draw in drawRect on a UITableViewCell... The deprecated code works when I am drawingRect on just a UIView.

Luke
  • 612
  • 1
  • 6
  • 19

4 Answers4

11

You shouldn't use UITableViewCell's drawRect method to perform custom drawing. The proper way to do it is to create a custom UIView and add it as a subview of your cell (as a subview of the contentView property). You can add the drawing code to this custom view and everything will work fine.

Hope this helps!

Check out these posts too:

Table View Cell custom drawing 1

Table View Cell custom drawing 2

Table View Cell custom drawing 3

Community
  • 1
  • 1
LuisCien
  • 6,362
  • 4
  • 34
  • 42
  • Cool man thank you... Is this just something recent in iOS7 not drawing in a UITableViewCell's drawRect? – Luke Sep 20 '13 at 01:29
  • You're welcome. This is not new to iOS 7; not sure when was introduced but I'd say it's been always like this. – LuisCien Sep 20 '13 at 05:09
  • The insets of the table are creating space on the left or right when using a UIView as the contentView in iOS 7..you will need to set the table's insets in your VC's viewWillLayoutSubviews like this: "self.yourTable.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);" – whyoz Oct 17 '13 at 17:46
7

As others said, don't use UITableViewCell's drawRect selector directly. By doing that, you're relying on implementation details of UITableViewCell, and Apple made no guarantee that such behaviour won't break in future versions, just as it did in iOS 7... Instead, create a custom UIView subclass, and add it as a subview to the UITableViewCell's contentView, like this:

@implementation CustomTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:[[CustomContentView alloc]initWithFrame:self.contentView.bounds]];
    }
    return self;
}

@end

And the CustomContentView:

@implementation CustomContentView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{
    NSDictionary * attributes = @{
                                  NSFontAttributeName : [UIFont fontWithName:@"Helvetica-bold" size:12],
                                  NSForegroundColorAttributeName : [UIColor blackColor]
                                  };

    [@"I <3 iOS 7" drawInRect:rect withAttributes:attributes];
}

@end

Works like charm!

Tamás Zahola
  • 9,271
  • 4
  • 34
  • 46
3

Try setting cell.backgroundColor = [UIColor clearColor] in init.

elp
  • 8,021
  • 7
  • 61
  • 120
Vincent
  • 29
  • 2
1

While I agree with the accepted answer, here's my take on it for the records:

If you don't need any of the builtin UITableViewCell functionality (swiping, removing, reordering, ...) and just use it as a container to draw your custom stuff, then you might want to consider removing all of the cells subviews in tableview:willDisplayCell:ForRowAtIndexPath. This will make your drawing be visible again and will get you maximum performance (since you get rid of the subviews you don't need).

DrMickeyLauer
  • 4,455
  • 3
  • 31
  • 67