Reputation: 801
I have a table view with a custom cell (governed by a custom class of course) that has the following drawRect:
function in the custom class:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGRect busNumberFrame = busNumber.frame;
NSLog(@"-----------------");
NSLog(@"Line Origin X: %f", busNumberFrame.origin.x + busNumberFrame.size.width);
NSLog(@"Line Origin Y: %f", self.frame.origin.y);
NSLog(@"Line End X: %f", busNumberFrame.origin.x + busNumberFrame.size.width);
NSLog(@"Line End Y: %f", self.frame.origin.y + self.frame.size.height);
NSLog(@"-----------------");
CGContextRef cgContext = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(cgContext, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(cgContext, 0.75);
CGContextMoveToPoint(cgContext, busNumberFrame.origin.x + busNumberFrame.size.width, self.frame.origin.y);
CGContextAddLineToPoint(cgContext, busNumberFrame.origin.x + busNumberFrame.size.width, self.frame.origin.y + self.frame.size.height);
CGContextStrokePath(cgContext);
}
This draws a line from the top of the cell to the bottom. This gets drawn successfully for the first cell but the subsequent cells don't show the line that's supposed to be drawn. Here is the code for cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BusInfoTableViewCell *cell = (BusInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Bus Route Cell"];
Bus *bus = [self.fetchedResultsController objectAtIndexPath:indexPath];
// Configure the cell...
cell.busNumber.text = [bus.number stringValue];
cell.firstStop.text = bus.departure;
cell.lastStop.text = bus.arrival;
[cell.contentView setNeedsDisplay];
return cell;
}
And here is the log output of drawRect:
:
2012-02-21 19:35:08.840 ETA[2208:707] -----------------
2012-02-21 19:35:08.843 ETA[2208:707] Line Origin X: 79.000000
2012-02-21 19:35:08.843 ETA[2208:707] Line Origin Y: 0.000000
2012-02-21 19:35:08.844 ETA[2208:707] Line End X: 79.000000
2012-02-21 19:35:08.845 ETA[2208:707] Line End Y: 63.000000
2012-02-21 19:35:08.845 ETA[2208:707] -----------------
2012-02-21 19:35:08.850 ETA[2208:707] -----------------
2012-02-21 19:35:08.851 ETA[2208:707] Line Origin X: 79.000000
2012-02-21 19:35:08.852 ETA[2208:707] Line Origin Y: 63.000000
2012-02-21 19:35:08.852 ETA[2208:707] Line End X: 79.000000
2012-02-21 19:35:08.853 ETA[2208:707] Line End Y: 126.000000
2012-02-21 19:35:08.853 ETA[2208:707] -----------------
2012-02-21 19:35:08.857 ETA[2208:707] -----------------
2012-02-21 19:35:08.857 ETA[2208:707] Line Origin X: 79.000000
2012-02-21 19:35:08.858 ETA[2208:707] Line Origin Y: 126.000000
2012-02-21 19:35:08.859 ETA[2208:707] Line End X: 79.000000
2012-02-21 19:35:08.859 ETA[2208:707] Line End Y: 189.000000
2012-02-21 19:35:08.860 ETA[2208:707] -----------------
I'm probably doing something wrong...
Any help is appreciated.
Thanks in advance.
Upvotes: 1
Views: 806
Reputation: 8526
The coordinates are local for the cell, so you should be using the following (all instances of self.frame
should be self.bounds
):
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGRect busNumberFrame = busNumber.frame;
NSLog(@"-----------------");
NSLog(@"Line Origin X: %f", busNumberFrame.origin.x + busNumberFrame.size.width);
NSLog(@"Line Origin Y: %f", self.bounds.origin.y);
NSLog(@"Line End X: %f", busNumberFrame.origin.x + busNumberFrame.size.width);
NSLog(@"Line End Y: %f", self.bounds.origin.y + self.bounds.size.height);
NSLog(@"-----------------");
CGContextRef cgContext = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(cgContext, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(cgContext, 0.75);
CGContextMoveToPoint(cgContext, busNumberFrame.origin.x + busNumberFrame.size.width, self.bounds.origin.y);
CGContextAddLineToPoint(cgContext, busNumberFrame.origin.x + busNumberFrame.size.width, self.bounds.origin.y + self.bounds.size.height);
CGContextStrokePath(cgContext);
}
Upvotes: 3