【问题标题】:UITableViewCell custom separator disappear during scroll ios8UITableViewCell自定义分隔符在滚动ios8期间消失
【发布时间】:2015-09-08 08:46:02
【问题描述】:

我已经搜索过,但我没有找到解决方案,我有一个带有 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];
}

有什么建议吗?谢谢

【问题讨论】:

  • 你试过 UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(90, self.contentView.frame.size.height - 1, 80, 1)]; ?
  • 不要在layoutSubviews方法中添加任何子视图,在初始化时添加视图并将其框架设置在layoutSubviews
  • @ArbenPnishi 谢谢我认为设置 self.contentView.frame.size.height - 1 解决了我的问题!没有考虑框架高度!

标签: ios objective-c uitableview custom-cell


【解决方案1】:

layoutSubviews 方法添加子视图的地方不对,因为它调用了很多次。在awakeFromNib 方法中添加这个子视图。

另外,您的行似乎超出了单元格,因为您使用的是self.contentView.frame.size.height尝试self.contentView.frame.size.height - 1

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-26
      • 2021-12-05
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多