自定义UIView子类的drawRect不会被调用内部的UITableViewCell
问题描述:
背景: 我有UITableViewCell
一个子类,其布局被定义为Storyboard的原型细胞。其中一个单元的IBOutlets
是UIView
的一个子类,我叫做BorderedProfileImageView
。这个类的drawRect:
功能重写如下:自定义UIView子类的drawRect不会被调用内部的UITableViewCell
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
CGContextSetLineWidth(context, 1.f);
CGFloat halfWidth = self.bounds.size.width/2;
NSInteger radius = sqrt(pow(halfWidth, 2) + pow(halfWidth, 2)) - 8.5;
CGContextAddArc(context,self.bounds.size.width/2,self.bounds.size.height/2,radius,0,2*3.1415926535898,1);
CGContextStrokePath(context);
}
这增加了蓝色环UIView的。
问题:
的蓝色戒指永远不会出现,并且BorderdProfileImageView
的drawRect:
功能不会被调用。单元格仍然绘制,BorderdProfileImageView
内部的UIImageView
被绘制。设置单元格时,我确实在BorderedProfileImageView
上拨打setNeedsDisplay
。
为什么drawRect:
没有被调用?
答
我想通了。
设置为BorderedProfileImageView
的IB中的对象最初是UIImageView
。将此对象更改为UIView
并重新链接已导致所需的行为。