使用CGContext绘制线条 [英] Draw Line using CGContext
问题描述
我想在表格视图单元格中绘制线条,以便我可以放置textfield&切换到单个单元格。我增加了细胞的高度。我怎样才能在细胞中划线?
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?
我有UIView的子类,其中包含以下代码
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);
然后我有一个tableViewController,带有分组表样式。在cellForRowAtIndexPath中我有以下代码
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
但它没有绘制任何行。我不能使用2个不同的细胞。我得帮助我。这是我第一次处理iphone的图形。谢谢
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
推荐答案
两件事......
首先,你通常不会自己进入细胞。
Two things... First, you usually don't draw into the cell itself.
您通常会进入内容视图。有时它有意义绘制到单元格的backgroundView或selectedBackgroundView。
You normally draw into the content view. Sometimes it makes sense draw into the cell's backgroundView or selectedBackgroundView.
[cell.contentView addSubview:drawLine];
其次,默认的单元格文本标签cell.textLabel和cell.detailTextLabel具有非透明背景。尝试将其背景颜色设置为 [UIColor clearColor]
。
Second, the default cell text labels cell.textLabel and cell.detailTextLabel have non-transparent background. Try setting their background colors to [UIColor clearColor]
.
编辑:还有一件事:你需要设置一个drawLine的正确框架
one more thing: you need to set a proper frame for your drawLine
DrawLineView *drawLine = [[DrawLineView alloc]initWithFrame:cell.contentView.bounds];
这篇关于使用CGContext绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!