在UITableViewCell上绘制垂直分隔符

时间:2012-05-12 13:02:52

标签: iphone objective-c uitableview

我有一个简单的UITableView样式,它的单元格是UITableViewCell的子类。

在单元子类中,我覆盖了drawRect以将此绘图代码放入(对于垂直分隔符):

CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(c, [[UIColor grayColor] CGColor]);
CGContextBeginPath(c);
CGContextMoveToPoint(c, self.frame.size.height + 0.5f, 0);
CGContextAddLineToPoint(c, self.frame.size.height + 0.5f, self.frame.size.height);
CGContextStrokePath(c);

效果很好。但是我现在已经将tableview样式更改为分组。这条线根本没有绘制。尽管设置了断点,但是调用了drawRect方法。

我想避免子类UIView只绘制一个小行,特别是因为我已经将tableview单元格子类化了,我只想在单元格上绘制。那么为什么代码突然停止在分组的tableview上工作呢?

1 个答案:

答案 0 :(得分:10)

drawRect子类中覆盖UITableViewCell不是一个好主意。如果您不想设置自定义单元格backgroundView,则可以添加1px宽度的简单UIView

UIView* vertLineView = [[UIView alloc] initWithFrame:CGRectMake(80, 0, 1, 44)];
vertLineView.backgroundColor = [UIColor redColor];
[self.contentView addSubview:vertLineView];
相关问题