gpt4 book ai didi

ios - tableView 单元格虚线边框未正确呈现

转载 作者:行者123 更新时间:2023-12-01 20:11:20 26 4
gpt4 key购买 nike

考虑我是 iOS 编程的新手。我试图将虚线边框显示为分隔符以及字段之间的垂直虚线边框,请查看屏幕截图以获取详细 View 。到目前为止没有问题,边框渲染正常。但是在tableView向上或向下滚动的那一刻,垂直虚线的位置发生了变化,请检查截图。我知道这是因为 cell reuse属性(property)。因为所有的单元格虚线创建方法都是​​一样的,所以我找不到 culprit line of code .我正在使用 ViewController并附上 UITableView到它,连接数据源和委托(delegate)。这是创建虚线的代码。

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

}

最佳答案

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

  • 你不断添加新层,你永远不会删除旧层,所以每次重复使用一个单元格都会占用更多内存,并且这些行可能在错误的位置
  • 当单元格大小发生变化时,您永远不会移动子层

  • 所以你需要几个不同的修复:
  • awakeFromNib 中添加单元格中的行, 所以它们只添加一次,就像您所有其他 View 都是
  • 覆盖 layoutSubviews在您的单元格中,并在每次调用时移动您添加的图层,以免它们与其他 View 重叠

  • 我实际上可能会放弃子层方法并使用 ImageView 作为线条。如果线条可能会发生很大变化,那么我可能会使用一个自定义 View 类,它有一个子层(根据您当前的代码)和如上所述的调整大小/布局逻辑。这样做的原因是您可以将自动布局约束应用于 View ,它会自动相对于单元格和您的其他 View 移动。

    您还应该删除
    if (cell==nil)
    {
    cell = [[KKCustList alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"custList"];
    }

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

    关于ios - tableView 单元格虚线边框未正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37656212/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com