问题描述:
前段时间做项目时,写了一个baseLineCell,这个cell的功能就是底部使用drawRect画了一根线,其它的cell只要继承它就不需要再画线了。本来在iOS8下一直工作的很好,哪知今天在iOS7的iPhone5C上面测试时,发现整个界面都变了,一个个cell下面都没有线了。我的代码如下:
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
if(!self.lineColor)
{
self.lineColor = RGB(221, 221, 221);
}
// Drawing code
//底部画一条线
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetLineWidth(context, 0.5);
CGContextMoveToPoint(context, 0, rect.size.height-0.25);
CGContextAddLineToPoint(context, rect.size.width, rect.size.height-0.25);
CGContextStrokePath(context);
}
代码检查来检查去,还是没发现什么问题,于是google,立马找到问题的解决办法http://stackoverflow.com/questions/18883975/cant-draw-in-uitableviewcells-drawrect。
解决办法:
iOS7下,cell.backgroundColor=[UIColor clearColor],应该是在iOS7下,cell的背景色是白色所以挡住了,而iOS8下背景色默认为clearColor,所以没问题。其实,在cell下使用drawRect里面画线,只要设置cell的背景色或者是contentView的背景色不透明,这条线就被挡住了。内部原因则暂时不深究,特此记录一下。