没有在所有uitableviewcells上绘制线条

时间:2012-02-19 22:56:14

标签: ios uitableview drawrect

我有一个带有自定义单元格的表格视图(当然由自定义类管理),在自定义类中具有以下drawRect:函数:

- (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);
}

这会从单元格的顶部到底部绘制一条线。这将成功绘制第一个单元格,但后续单元格不显示应该绘制的线条。以下是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;
}

以下是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] -----------------

我可能做错了什么......

感谢任何帮助。

提前致谢。

1 个答案:

答案 0 :(得分:3)

坐标是单元格的本地坐标,因此您应该使用以下内容(self.frame的所有实例都应为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);
}