在UITableViewCell上绘制垂直分隔符

Jon*_*an. 5 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);
Run Code Online (Sandbox Code Playgroud)

它运作得很好.但是我现在已经将tableview样式更改为分组.这条线根本没有绘制.虽然设置断点显示调用drawRect方法.

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

one*_*ray 10

drawRectUITableViewCell子类中重写是不好的主意.如果您不想设置自定义单元格backgroundView,那么您只需添加UIView1px宽度的简单.

UIView* vertLineView = [[UIView alloc] initWithFrame:CGRectMake(80, 0, 1, 44)];
vertLineView.backgroundColor = [UIColor redColor];
[self.contentView addSubview:vertLineView];
Run Code Online (Sandbox Code Playgroud)