tableView单元格虚线边框无法正确呈现

时间:2016-06-06 11:26:46

标签: ios objective-c uitableview

考虑我在iOS编程中的新手。我试图将虚线边框显示为分隔符以及字段之间的垂直虚线边框,请查看屏幕截图以获取详细视图。直到现在没有问题,边框正确渲染。但是当tableView向上或向下滚动时,垂直虚线的位置会发生变化,请检查截图。我知道这是由于cell reuse属性而发生的。对于所有单元格虚线创建方法都是​​一样的,我无法找到culprit line of code。我正在使用ViewController并附加UITableView,连接数据源和委托。这是虚线创建的代码。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
KKCustList *cell = [tableView dequeueReusableCellWithIdentifier:@"custList" forIndexPath:indexPath];

if (cell==nil)
{
    cell = [[KKCustList alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"custList"];
}
/** Creating dotted lines for border **/


CAShapeLayer *shapelayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPath];

[path moveToPoint:CGPointMake(0, cell.frame.size.height)];
[path addLineToPoint:CGPointMake(cell.frame.size.width, cell.frame.size.height)];
shapelayer.strokeStart = 0.0;
shapelayer.strokeColor = [UIColor firstRowColor].CGColor;
shapelayer.lineWidth = 2.0;
shapelayer.lineJoin = kCALineJoinRound;
shapelayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2 ], nil];
shapelayer.path = path.CGPath;
[cell.layer addSublayer:shapelayer];

/** creating vertical dotted line **/

CAShapeLayer *shapelayer1 = [CAShapeLayer layer];
UIBezierPath *path1 = [UIBezierPath bezierPath];

[path1 moveToPoint:CGPointMake(cell.soLabel.bounds.origin.x + cell.soLabel.bounds.size.width, cell.soLabel.bounds.origin.y + 10)];
[path1 addLineToPoint:CGPointMake(cell.soLabel.bounds.origin.x + cell.soLabel.bounds.size.width, cell.soLabel.bounds.size.height)];
shapelayer1.strokeStart = 0.0;
shapelayer1.strokeColor = [UIColor firstRowColor].CGColor;
shapelayer1.lineWidth = 1.0;
shapelayer1.lineJoin = kCALineJoinRound;
shapelayer1.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2 ], nil];
shapelayer1.path = path1.CGPath;
[cell.layer addSublayer:shapelayer1];

Desired
after scrolling

更新 将代码移动到awakeFromNib

@implementation KKCustList
@synthesize shapelayer1;

- (void)awakeFromNib {

[super awakeFromNib];

 /** creating vertical dotted line **/

    CAShapeLayer *shapelayer1 = [CAShapeLayer layer];
    UIBezierPath *path1 = [UIBezierPath bezierPath];

    [path1 moveToPoint:CGPointMake(self.soLabel.bounds.origin.x + self.soLabel.bounds.size.width, self.soLabel.bounds.origin.y + 10)];
    [path1 addLineToPoint:CGPointMake(self.soLabel.bounds.origin.x + self.soLabel.bounds.size.width, self.soLabel.bounds.size.height)];
    shapelayer1.strokeStart = 0.0;
    shapelayer1.strokeColor = [UIColor firstRowColor].CGColor;
    shapelayer1.lineWidth = 1.0;
    shapelayer1.lineJoin = kCALineJoinRound;
    shapelayer1.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2 ], nil];
    shapelayer1.path = path1.CGPath;
    [self.contentView.layer addSublayer:shapelayer1];

}

1 个答案:

答案 0 :(得分:2)

您当前的代码有几个问题:

  1. 你不断添加新图层,你永远不会删除旧图层,因此每次重复使用一个单元格时,它会占用更多内存并且线条可能位于错误的位置
  2. 当单元格大小更改时,您永远不会移动子图层
  3. 所以你需要几个不同的修复:

    1. awakeFromNib中添加单元格中的行,因此只需添加一次,就像其他所有观点一样
    2. 覆盖单元格中的layoutSubviews并移动每次调用时添加的图层,以便它们不会与其他视图重叠
    3. 我实际上可能会删除子图层方法并使用图像视图。如果这些行可能会发生很大变化,那么我可能会使用一个自定义视图类,它具有一个子层(根据您当前的代码)和调整大小/布局逻辑,如上所述。这样做的原因是您可以将自动布局约束应用于视图,它将自动相对于单元格和其他视图移动。

      您还应该删除

      if (cell==nil)
      {
          cell = [[KKCustList alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"custList"];
      }
      

      因为永远不会使用它,所以将始终为您创建一个单元格。