UITableViewCell drawInRect 在iOS7中失败
来源:互联网 发布:知乎论坛特点 编辑:程序博客网 时间:2024/06/04 20:05
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 thecontentView
property). You can add the drawing code to this custom view and everything will work fine.
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!
- UITableViewCell drawInRect 在iOS7中失败
- 关于在XCode6(iOS8)UITableViewCell与iOS7中UITableViewCell的问题
- iOS7下的drawInRect的新方法drawInRect:withAttributes:使用
- UITableViewCell 在iOS7上的变化
- IOS7 中 UITableViewCell 删除视图不还原
- UITableViewCell 在IOS7的父视图是UITableViewWrapperView。
- UITableViewCell 在IOS7的父视图是UITableViewWrapperView。
- UITableViewCell 在IOS7的父视图是UITableViewWrapperView。
- UITableViewCell 在IOS7的父视图是UITableViewWrapperView
- UITableViewCell drawRect画线在iOS7下不显示的问题
- iOS7中UITableView问题以及UITableViewCell的层级关系
- iOS7中UITableView问题以及UITableViewCell的层级关系
- iOS7中UITableView问题以及UITableViewCell的层级关系
- iOS7 UITableViewCell 线偏移
- iOS7 UITableViewCell 线偏移
- iOS7 UITableViewCell适配
- ios7 UITableViewCell高度自适应
- 在程序运行中调整UITableViewCell高度
- 一个有趣的C问题
- 自定义控件 BUTTON的实现。
- 关于BAPI创建FI凭证时 原因代码等字段的传入
- 电脑故障(一)
- Geeks 面试题:Binomial Coefficient
- UITableViewCell drawInRect 在iOS7中失败
- 百度“百发”VS阿里“余额宝”
- 题目1073:杨辉三角形
- 阿里巴巴集团去IOE运动的思考与总结
- Palindrome Partitioning
- 认识电容及电容电阻的标注
- 南邮-1988-有序集合
- 15、UIWindow-OC+UI
- java笔记day_3