alessioarsuffi
alessioarsuffi

Reputation: 561

UITableViewCell custom separator disappear during scroll ios8

i've searched but i haven't found a solution to this, i have a tableview with uitableviewcell. To the cell i need to apply this custom separator:

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(90, self.contentView.frame.size.height, 80, 1)];

lineView.backgroundColor = [UIColor lightGrayColor];
[self.contentView addSubView:lineView];

and the separator is displayed correctly, now, i don't know why if i scroll medium fast up and down tableview, the separator disappear on certain cell. I tried to set by:

 - (void)layoutSubviews
{
    [super layoutSubviews];

    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(90, self.contentView.frame.size.height, 80, 1)];

    lineView.backgroundColor = [UIColor lightGrayColor];

    [self.contentView addSubview:lineView];
}

any suggestion? thanks

Upvotes: 1

Views: 503

Answers (2)

Shankar BS
Shankar BS

Reputation: 8402

u haven't shown any code about how u are creating the cell but i will give a sample example u can do like it, for example

//during initialisation
- (instancetype)initWithFrame:(CGRect)frame
 {
   self = [super initWithFrame:frame];
  if(self)
  {
      [self setUpCell]; 
  }
  return self;
} 

- (void)awakeFromNib
{
   [self setUpCell];
}

//hear add the views only once 
- (void)setUpCell
{
  //hear add the all views
  UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(90, self.contentView.frame.size.height - 1, 80, 1)];
  lineView.backgroundColor = [UIColor greenColor];
  lineView.tag = 123; //set its tag to access it in "layoutsubviews"
  [self.contentView addSubview:lineView];    
}

//this method may be called repeatedly, just set the frames of the subviews hear 
- (void)layoutSubviews
{
   [super layoutSubviews];
   UIView *lineView = [self.contentView viewWithTag:123]; //get the subview with tag
   lineView.frame = CGRectMake(90, self.contentView.bounds.size.height - 1,self.contentView.bounds.size.height - 1, 1);
}

Upvotes: 0

AlexZd
AlexZd

Reputation: 2152

layoutSubviews method is wrong place to add subviews, because it calls many times. Add this subview in awakeFromNib method.

Also it seems that your line out of cell, because you are using self.contentView.frame.size.height try self.contentView.frame.size.height - 1

Moreover try to test it on device, sometimes simulator has similar graphic bugs.

Upvotes: 1

Related Questions