使用 CGContext 绘制线条
我想在表视图单元格中画线,以便可以放置文本字段和文本字段。在单细胞中切换。我增加了单元格的高度。如何在单元格中画线?
我有 UIView 的子类,其中包含以下代码
//Get the CGContext from this view
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the stroke (pen) color
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//Set the width of the pen mark
CGContextSetLineWidth(context, 1.0);
// Draw a line
//Start at this point
CGContextMoveToPoint(context, 10.0, 30.0);
//Give instructions to the CGContext
//(move "pen" around the screen)
CGContextAddLineToPoint(context, 310.0, 30.0);
//Draw it
CGContextStrokePath(context);
然后我有一个具有分组表格样式的 tableViewController 。在 cellForRowAtIndexPath 中,我有以下代码
//code to add textfield
DrawLineView *drawLine = [[DrawLineView alloc]init];
[cell addSubview:drawLine];
//code add switch
,但它没有绘制任何线条。我不能使用 2 个不同的电池。我必须请帮助我。这是我第一次在 iPhone 上处理图形。谢谢
I want to draw line in table view cell so that I can place textfield & switch in single cell. I increased the height of cell. How can I draw line in cell?
I have subclass of UIView which contains following code
//Get the CGContext from this view
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the stroke (pen) color
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//Set the width of the pen mark
CGContextSetLineWidth(context, 1.0);
// Draw a line
//Start at this point
CGContextMoveToPoint(context, 10.0, 30.0);
//Give instructions to the CGContext
//(move "pen" around the screen)
CGContextAddLineToPoint(context, 310.0, 30.0);
//Draw it
CGContextStrokePath(context);
Then I have a tableViewController with grouped table style. In cellForRowAtIndexPath I have following code
//code to add textfield
DrawLineView *drawLine = [[DrawLineView alloc]init];
[cell addSubview:drawLine];
//code add switch
But it is not drawing any line. I can't use 2 different cells. I have to Please help me. This is my first to deal with graphics i iphone. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想画一条线,那么最好使用 CAShapeLayer,向其传递一条带有线的路径,然后将其附加为单元格内容视图的子图层。该表的性能应该比使用带有自定义绘制矩形的视图更好。
通过 CALayer 和路径绘制线条的示例:
If all you want to do is draw a line, it would be a lot better to use a CAShapeLayer, pass it a path with a line, and then attach that as a sublayer of the cells content view. The table should perform better than using a view with a custom drawRect.
Example of drawing a line via CALayer and a path:
有两件事...
首先,您通常不会绘制单元格本身。
您通常会绘制到内容视图中。有时,绘制到单元格的backgroundView或selectedBackgroundView中是有意义的。
其次,默认的单元格文本标签cell.textLabel和cell.detailTextLabel具有不透明的背景。尝试将其背景颜色设置为
[UIColor clearColor]
。编辑:还有一件事:你需要为你的画线设置一个合适的框架
Two things...
First, you usually don't draw into the cell itself.
You normally draw into the content view. Sometimes it makes sense draw into the cell's backgroundView or selectedBackgroundView.
Second, the default cell text labels cell.textLabel and cell.detailTextLabel have non-transparent background. Try setting their background colors to
[UIColor clearColor]
.Edit: one more thing: you need to set a proper frame for your drawLine
我认为绘制线条的一种非常简单的方法是创建一个 UIView 并用所需的颜色填充它,然后相应地选择其宽度,并将高度设置为 1 像素,如下所示:
I think a very simplified way of drawing a line is to create a UIView and fill it with desired color, then choose its width accordingly, and setup height to be 1 pixel like so: