ios - 未在所有 uitableviewcells 上绘制线

标签 ios uitableview drawrect

我有一个带有自定义单元格的表格 View (当然由自定义类管理),它具有以下 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] -----------------

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

任何帮助表示赞赏。

提前致谢。

最佳答案

坐标是单元格的本地坐标,因此您应该使用以下坐标(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);
}

关于ios - 未在所有 uitableviewcells 上绘制线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9354123/

相关文章:

ios - 带有阴影、圆角和自定义 drawRect 的 UIView

ios - 如何保存 pushNotification 附带的两个项目

ios - 如何使用 NSLayoutConstraint 限制 UIButton 高度

ios - Swift 3.0 今天扩展了具有动态高度的紧凑模式

ios - 单元格未出现在 UITableView 中 - Swift 3

ios - 内存泄漏,但 CGContextRelease 破坏了 View

ios - 如何在计时器暂停时捕获耗时

swift - 关联枚举案例将类名

ios - 在 Ios 中,当项目源在 xamarin.forms 中更新时,ListView 会自动滚动到顶部。在ios中保持滚动位置

ios - 按下按钮时更改自定义 UIButton (CoreGraphics) 的背景颜色