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

时间:2015-09-08 08:46:02

标签: ios objective-c uitableview custom-cell

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

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

有什么建议吗?感谢

2 个答案:

答案 0 :(得分:1)

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

此外,您的行似乎已离开单元格,因为您正在使用self.contentView.frame.size.height尝试self.contentView.frame.size.height - 1

此外,尝试在设备上测试它,有时模拟器有类似的图形错误。

答案 1 :(得分:0)

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

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