代码之家  ›  专栏  ›  技术社区  ›  Durgaprasad

iphone-设置背景视图时,在UITableViewCell中画一条线

  •  2
  • Durgaprasad  · 技术社区  · 11 年前

    我把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;
    

    问题出在哪里?背景视图如何隐藏线条? 请帮忙

    3 回复  |  直到 11 年前
        1
  •  1
  •   calimarkus    11 年前

    我猜你在 UITableViewCell ?

    您不应该覆盖 drawRect 细胞本身。而是将图形代码放在自定义中 backgroundView ,或在 contentView 等级制度(取决于您的计划结果,可能backgroundView对您来说是正确的)

    线路断了,因为 背景视图 是TableViewCell的子视图,因此它位于单元格本身的顶部。(如果您使用 [UIColor colorWithWhite:0.0 alpha:0.5] 作为背景背景背景视图的颜色。)UITableViewCell上有许多视图,看起来有点像这样:

    UITableViewCell
      > BackgroundView
      > (EditControl) 
      > ContentView
      > (AccessoryView)
    
        2
  •  0
  •   Firdous    11 年前

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

        3
  •  0
  •   Himanshu    11 年前

    当你已经绘制了该单元格的背景时,你不能再为该单元格设置背景颜色或图像。它会覆盖你绘制的内容。