UITableViewCell drawInRect iOS7

Hi, I am trying to draw strings in a 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 cannot make something appear ... self.playerName is not nil, and playerNameRect is correct.

I used the following code earlier to do the same, but recently deprecated in iOS 7

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

It is also strange that I cannot get anything to draw drawRect on a UITableViewCell ... Deprecated code works when I draw a Rect only on a UIView.

+4
source share
4 answers

You should not use the UITableViewCell drawRect method to execute a custom drawing. The right way to do this is to create a custom UIView and add it as a subitem of your cell (as a subheading of the contentView property). You can add the drawing code to this custom view, and everything will work fine.

Hope this helps!

Also check these messages:

Custom drawing with a list of tables 1

Custom drawing with a list of tables 2

Custom drawing with a list of tables 3

+11
source

As others have said, do not use the drawRect UITableViewCell selector directly. In doing this, you rely on implementation details of UITableViewCell, and Apple does not guarantee that this behavior will not be violated in future versions, as it was in iOS 7 ... Instead, create a custom subclass of UIView and add this as a submission of the UITableViewCell contentView, for example:

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

It works like a charm!

+7
source

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

+3
source

While I agree with the accepted answer, here I take it for the notes:

If you do not need any built-in UITableViewCell function (scrolling, deleting, reordering, etc.) and just use it as a container for drawing custom materials, then you may need to remove all subviews cells in tableview: willDisplayCell: ForRowAtIndexPath. This will make your drawing visible again and provide maximum performance (since you will get rid of sub-views that you don't need).

+1
source

All Articles