UITableViewCell drawInRect iOS7

我们两清 提交于 2019-12-05 02:07:34

问题


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.


回答1:


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




回答2:


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!




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/18905917/uitableviewcell-drawinrect-ios7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!