简体   繁体   English

如何在UITableViewCell中绘制两个平行条

[英]How to draw two parallel strips in a UITableViewCell

I need to customize the cells of a table creating two transparent strips, one in correspondence of the upper edge of UITableViewCell, and other in correspondence of the lower edge. 我需要自定义表格的单元格,以创建两个透明条,一个对应于UITableViewCell的上边缘,另一个对应于下边缘。 These strips should be transparent to see the color of the view below (light yellow). 这些条应该是透明的,以查看下面视图的颜色(浅黄色)。 I created a subclass of UITableViewCell and I built the method LayoutSubviews() to draw the strips, it is wrong? 我创建了UITableViewCell的子类,并构建了LayoutSubviews()方法来绘制条带,这是错误的吗? I get this errors: 我得到这个错误:

   <Error>: CGContextBeginPath: invalid context 0x0
   <Error>: CGContextMoveToPoint: invalid context 0x0
   <Error>: CGContextAddLineToPoint: invalid context 0x0
   <Error>: CGContextSetLineWidth: invalid context 0x0
   <Error>: CGContextSetFillColorWithColor: invalid context 0x0
   <Error>: CGContextMoveToPoint: invalid context 0x0
   <Error>: CGContextAddLineToPoint: invalid context 0x0
   <Error>: CGContextSetLineWidth: invalid context 0x0
   <Error>: CGContextSetFillColorWithColor: invalid context 0x0

This is the code in CustomCell.m: 这是CustomCell.m中的代码:

 -(void) layoutSubviews{
   [super layoutSubviews];


   CGContextRef ctxt = UIGraphicsGetCurrentContext();
   CGContextBeginPath(ctxt);
   CGContextMoveToPoint(ctxt, self.bounds.origin.x, self.bounds.origin.y);
   CGContextAddLineToPoint(ctxt, self.bounds.size.width, self.bounds.origin.y);
   CGContextSetLineWidth(ctxt, 5);
   CGContextSetFillColorWithColor(ctxt, [UIColor clearColor].CGColor); 

   CGContextMoveToPoint(ctxt, self.bounds.origin.x, self.bounds.size.height);
   CGContextAddLineToPoint(ctxt, self.bounds.size.width, self.bounds.size.height);
   CGContextSetLineWidth(ctxt, 5);
   CGContextSetFillColorWithColor(ctxt, [UIColor clearColor].CGColor);
   CGContextStrokePath(ctxt);


}

layoutSubviews is the wrong method to draw something. layoutSubviews是错误的绘制方法。 You don't have a drawing context there. 您那里没有绘图环境。 Move your code to drawRect: : 将代码移到drawRect: ::

- (void)drawRect:(CGRect)rect {
    [super drawRect: rect];


   CGContextRef ctxt = UIGraphicsGetCurrentContext();
   CGContextBeginPath(ctxt);
   CGContextMoveToPoint(ctxt, self.bounds.origin.x, self.bounds.origin.y);
   CGContextAddLineToPoint(ctxt, self.bounds.size.width, self.bounds.origin.y);
   CGContextSetLineWidth(ctxt, 5);
   CGContextSetFillColorWithColor(ctxt, [UIColor clearColor].CGColor); 

   CGContextMoveToPoint(ctxt, self.bounds.origin.x, self.bounds.size.height);
   CGContextAddLineToPoint(ctxt, self.bounds.size.width, self.bounds.size.height);
   CGContextSetLineWidth(ctxt, 5);
   CGContextSetFillColorWithColor(ctxt, [UIColor clearColor].CGColor);
   CGContextStrokePath(ctxt);
}
(void) drawRect:(CGRect)rect
{
  CGContextRef context = UIGraphicsGetCurrentContext();
  UIColor *color = [UIColor colorWithRed:0 green:1 blue:0 alpha:0];
  CGContextSetFillColorWithColor(context, color.CGColor);

  CGContextSetLineWidth(context, 3.0);
  CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);     //CGContextSetRGBFillColor doesn't work either
  CGContextBeginPath(context);
  CGContextMoveToPoint(context, 100.0, 60.0);
  CGRect rectangle = {100.0, 60.0, 120.0, 120.0};
  CGContextAddRect(context, rectangle);

  CGContextStrokePath(context);
  CGContextFillPath(context);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM