我有一个带有自定义单元格的表格 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/