UITableViewCell 自定义分隔符在滚动期间消失 ios8

UITableViewCell custom separator disappear during scroll ios8

我已经搜索过了,但我还没有找到解决方案,我有一个带有 uitableviewcell 的 tableview。对于我需要应用此自定义分隔符的单元格:

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

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

并且分隔符显示正确,现在,我不知道为什么如果我以中等速度快速上下滚动表格视图,分隔符会在某些单元格上消失。我尝试设置为:

 - (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];
}

有什么建议吗?谢谢

layoutSubviews 方法是添加子视图的错误地方,因为它调用了很多次。在 awakeFromNib 方法中添加此子视图。

而且你的行似乎超出了单元格,因为你正在使用 self.contentView.frame.size.height 试试 self.contentView.frame.size.height - 1

另外尝试在设备上进行测试,有时模拟器也有类似的图形错误。

你没有显示任何关于你如何创建单元格的代码,但我会给出一个你可以喜欢的示例,例如

//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);
}