UITableViewCells尝试绘制的无效上下文

时间:2011-04-05 12:59:00

标签: ios uitableview cgcontext

我试图在UITableViewCell中绘制一个复选框三角形。 在数据源协议方法-tableView:cellForRowAtIndexPath中:我在代码块中添加了以下内容,询问单元格是否为零:

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 75,10);
    CGContextAddLineToPoint(context, 10, 150);
    CGContextAddLineToPoint(context, 160, 150);
    CGContextClosePath(context);
    [[UIColor blackColor] setStroke];
    CGContextStrokePath(context);

执行代码时,我在以下方法中得到一系列“无效上下文0x0”消息: CGContextBeginPath: CGContextMoveToPoint: CGContextAddLineToPoint :(两次) CGContextClosePath: CGContextSetStrokeColorWithColor: CGContextDrawPath

对于表格中的每个TableViewCell行,重复这些消息。

我无法在customizedUITableViewCell对象中绘制三角形或其他几何形状吗?

1 个答案:

答案 0 :(得分:1)

如果在UIGraphicsGetCurrentContext()之外调用,

drawRect:将不会返回有效的上下文。您必须在视图的drawRect:方法中进行自定义绘图。为了您的目的,您应该为您的复选框创建一个自定义UIView子类,并将该视图作为子视图添加到单元格中。