您好我正在尝试使用以下代码在iOS 7中的UITableViewCell中绘制字符串
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]; }但是我无法得到任何东西......自我.playerName不是nil,并且playerNameRect是正确的。
However I can not get anything to appear... self.playerName is not nil, and the playerNameRect is correct.
我以前使用以下代码执行相同的操作但最近在iOS 7中已弃用
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];同样奇怪的是我无法在UITableViewCell上绘制drawRect ...当我在一个UIView上使用drawRect时,不推荐使用的代码可以工作。
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.
推荐答案你不应该使用 UITableViewCell 's drawRect 执行自定义绘图的方法。正确的方法是创建一个自定义 UIView 并将其添加为您的单元格的子视图(作为 contentView的子视图 property)。您可以将绘图代码添加到此自定义视图中,一切正常。
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.
希望这会有所帮助!
查看这些帖子:
表视图单元格自定义图纸1
表视图单元格自定义绘图2
表格查看单元格自定义图纸3
更多推荐
UITableViewCell drawInRect iOS7
发布评论