-2

Trying to draw a rect in 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));
}

Does anybody know why this doesn't work in iOS 7 but does in iOS 6?

Matt S.
  • 13,305
  • 15
  • 73
  • 129
Praveen-K
  • 3,401
  • 1
  • 23
  • 31

2 Answers2

1

I had the same issue under iOS 7 - anything you draw in your -drawRect method gets obscured by the cell's subviews. Instead, add an instance of a new view subclass as a subview to your cells contentView and do the drawing there.

See this and this. If you don't want to crate a custom subclass, you could use block drawing views instead.

Community
  • 1
  • 1
MrMage
  • 7,282
  • 2
  • 41
  • 71
0

I have fixed it by adding a subview to 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];
}
Praveen-K
  • 3,401
  • 1
  • 23
  • 31