1

テーブルのセルをカスタマイズして、2つの透明なストリップを作成する必要があります。1つはUITableViewCellの上端に対応し、もう1つは下端に対応します。これらのストリップは、下のビューの色(明るい黄色)を確認するために透明である必要があります。UITableViewCellのサブクラスを作成し、ストリップを描画するためのメソッドLayoutSubviews()を作成しましたが、間違っていますか?このエラーが発生します:

   <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

これは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);


}
4

2 に答える 2

3

layoutSubviews何かを描くのは間違った方法です。そこには描画コンテキストがありません。コードを次の場所に移動します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);
}
于 2012-10-22T10:48:54.200 に答える
2
(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);
}
于 2012-12-29T05:35:33.630 に答える