Search code examples
iosuitableviewcore-animation

Where should I add CALayers to UITableViewCell?


I have a very simple CAShapelayer I want to add to a custom UITableViewCell but I've never been sure of the best place to do so.

CAShapeLayer *line = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path addLineToPoint:CGPointMake(0, self.bounds.size.height)];
[line setPath:path.CGPath];
[line setLineWidth:1];
[line setStrokeColor:[UIColor blackColor].CGColor];

Should I add this in some sort of common init? or in prepareForReuse or in the dataSource method? Should I make it a @property and check for it in drawRect?


Solution

  • I think it should be in the appropriate init method (which ever is called based on where the cell is made). This will ensure that the code is only run once when the cell is initialized, and not repeatedly as it would in data source methods.