-2

试图在 UITableViewCell 中绘制一个矩形

//Works with iOS6 and earlier but NOT with ( iOS7 )

 - (void)drawRect:(CGRect)rect {
    // Creating a black border
    [[UIColor blackColor] setFill];
    UIRectFill(CGRectMake(10, 5, 40, 43));

    // Filling with rig color
    [[UIColor colorWithRed:r green:g blue:b alpha:a] setFill];
    UIRectFill(CGRectMake(11, 6, 38, 41));
}

有谁知道为什么这在 iOS 7 中不起作用但在 iOS 6 中起作用?

4

2 回答 2

1

我在 iOS 7 下遇到了同样的问题 - 您在-drawRect方法中绘制的任何内容都会被单元格的子视图遮盖。相反,将新视图子类的实例作为子视图添加到您的单元格contentView并在那里进行绘图。

看到这个这个。如果您不想创建自定义子类,则可以改用块绘图视图

于 2013-10-11T09:36:59.990 回答
0

我已经通过向 contentview 添加子视图来修复它

- (id)initWithStyle:(UITableViewCellStyle)style 
                              reuseIdentifier:(NSString *)reuseIdentifier{
      if(!self)
         return self;

      self.colorView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 40, 43)];
      self.colorView.layer.borderColor = [[UIColor blackColor] CGColor];
      self.colorView.layer.borderWidth = 1.0;
      [self.contentView addSubview:self.colorView];

  }

- (void)setActivityColor:(UIColor*)color
{
    [self.colorView setBackgroundColor:color];
}
于 2013-10-11T10:27:21.813 回答