Draw vertical separator on UITableViewCell

iphone, objective-c, uitableview

Solution

It is not good idea to override `drawRect` in `UITableViewCell` subclasses. If you do not want to set custom cell's `backgroundView`, then you can just add simple `UIView` of 1px width.

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

Problem

I had a plain styled UITableView, and it's cells are a subclass of UITableViewCell. In the cell subclass I overrode drawRect to put this drawing code in (for a vertical separator): ``` 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); ``` It worked great. However I have now changed the tableview style to grouped. The line simply isn't drawn. Although settings a breakpoint shows the drawRect method is called. I would like to avoid subclasses UIView just to draw a small line, especially as I've already subclassed the tableview cell and I just want to draw on the cell. So why does the code suddenly stop working on a grouped tableview?

Original source