我想在表视图单元格中画线,以便可以将文本字段和开关放置在单个单元格中。我增加了单元格的高度。如何在单元格中画线?
我有 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 上处理图形。谢谢
如果您只想画一条线,那么最好使用 CAShapeLayer,向其传递一条带有线的路径,然后将其附加为单元格内容视图的子图层。该表的性能应该比使用带有自定义绘制矩形的视图更好。
通过 CALayer 和路径绘制线条的示例:
// You'll also need the QuartzCore framework added to the project
#import <QuartzCore/QuartzCore.h>
CAShapeLayer *lineShape = nil;
CGMutablePathRef linePath = nil;
linePath = CGPathCreateMutable();
lineShape = [CAShapeLayer layer];
lineShape.lineWidth = 4.0f;
lineShape.lineCap = kCALineCapRound;;
lineShape.strokeColor = [[UIColor blackColor] CGColor];
int x = 0; int y = 0;
int toX = 30; int toY = 40;
CGPathMoveToPoint(linePath, NULL, x, y);
CGPathAddLineToPoint(linePath, NULL, toX, toY);
lineShape.path = linePath;
CGPathRelease(linePath);
[self.layer addSublayer:lineShape];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)