2

我写了如下的drawRect。

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    CGContextRef cxt = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(cxt, 2.0);
    CGContextSetStrokeColorWithColor(cxt, [UIColor redColor].CGColor);
    CGContextMoveToPoint(cxt, 250.0 , 0.0);
    CGContextAddLineToPoint(cxt, 250.0, 50.0);
   CGContextStrokePath(cxt);
}

它画了红线。但是当我将背景视图设置为单元格线时消失了。我已将视图设置如下。

UIView *view = [[UIView alloc] initWithFrame:cell.frame];
        view.backgroundColor = [UIColor grayColor];
    cell.backgroundView = view;

问题是什么?背景视图如何隐藏线条?请帮忙

4

3 回答 3

1

我猜你在一个UITableViewCell

您不应该覆盖drawRect单元格本身。而是将您的绘图代码放在 custom或层次结构backgroundView中的自定义视图中。contentView(取决于您计划的结果,可能 backgroundView 对您来说是正确的)

这条线消失了,因为backgroundView它是 TableViewCell 的子视图,所以它位于单元格本身的顶部。(你可以看到这个,如果你使用[UIColor colorWithWhite:0.0 alpha:0.5]你的 backgroundView 的 backgroundColor。) UITableViewCell 上有很多视图,它看起来有点像这样:

UITableViewCell
  > BackgroundView
  > (EditControl) 
  > ContentView
  > (AccessoryView)
于 2013-04-24T09:48:18.377 回答
0

同意 jaydee3,你不应该覆盖 UITableViewCell 的 drawRect,而是让你的自定义视图类从 UIView 扩展,在那里扩展 drawRect 并在那里做所有的框架和着色的事情,然后将该视图的一个实例设置为你的单元格背景视图,它更好方法

于 2013-04-24T09:53:18.770 回答
0

当您已经绘制了背景时,您无法再次为该单元格设置背景颜色或图像。它覆盖在您绘制的内容上。

于 2013-04-24T11:04:08.120 回答