UITableViewCells无效上下文试图绘制 [英] UITableViewCells invalid context trying to draw
问题描述
我试图在UITableViewCell中绘制一个复选框三角形。
在数据源协议方法-tableView:cellForRowAtIndexPath中:我在代码块中添加了以下内容,询问该单元格是否为零:
I am trying to draw a checkbox triangle in a UITableViewCell. In the Data Source protocol method -tableView:cellForRowAtIndexPath: I have added the following in the code bloc which asks if the cell is nil:
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
When executing the code, I get a series of 'invalid context 0x0' messages on the following methods: CGContextBeginPath: CGContextMoveToPoint: CGContextAddLineToPoint: (twice) CGContextClosePath: CGContextSetStrokeColorWithColor: CGContextDrawPath
消息对于表中的每个TableViewCell行都重复。
The messages repeat for each TableViewCell row in my table.
我无法在自定义的UITableViewCell对象中绘制三角形或其他几何形状吗?
Am I not able to draw triangles or other geometric shapes within customizedUITableViewCell objects?
推荐答案
UIGraphicsGetCurrentContext()
如果在外部调用将不会返回有效上下文drawRect:
。您必须在视图的 drawRect:
方法中进行自定义绘制。为了您的目的,您可能应该为复选框创建一个自定义的 UIView
子类,然后将该视图作为子视图添加到该单元格中。
UIGraphicsGetCurrentContext()
will not return a valid context if called outside drawRect:
. You have to do your custom drawing in a view's drawRect:
method. For your purpose, you should probably create a custom UIView
subclass for your checkbox and add that view as a subview to the cell.
这篇关于UITableViewCells无效上下文试图绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!