UiView上的UIBezierPath不会更新,直到滚动tableview

时间:2017-12-07 19:43:05

标签: objective-c uitableview

我已将UIView添加到Tableview的单元格中,并通过以下方式给出了底部和右侧的beizer路径:

UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0,
                          0,
                          cell.bgView.frame.size.width -2+ shadowSize,
                          cell.bgView.frame.size.height+1 + shadowSize)];
    cell.bgView.layer.masksToBounds = NO;
    cell.bgView.layer.shadowColor = [[UIColor colorWithRed:186.0/255.0 green:0.0/255.0 blue:231.0/255.0 alpha:1.0f]CGColor];
    cell.bgView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    cell.bgView.layer.shadowOpacity = 0.7f;
    cell.bgView.layer.shadowPath = shadowPath.CGPath;

第一次,当tableview重新加载时,bezier路径没有正确加载

See Picture 1

但是当我向上/向下滚动时它看起来很完美

See Picture 2

我尝试过各种可能的解决方案,即

dispatch_async(dispatch_get_main_queue(), ^{
                [self.topRatedTable reloadData];
            });

 [cell layoutIfNeeded];
    [cell updateConstraintsIfNeeded];

但没有任何作用。

2 个答案:

答案 0 :(得分:2)

您的单元格及其子视图(例如,我认为,bgView)在布局运行之前没有最终帧。如果您的路径创建代码位于tableView:cellForRowAtIndexPath:方法中,那么您在布局运行之前就会访问cell.bgView.frame

在自定义UITableViewCell子类中,覆盖layoutSubviews并在调用shadowPath后设置super

// In your custom `UITableViewCell` subclass:
- (void)layoutSubviews {
    [super layoutSubviews];
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0,
                      0,
                      cell.bgView.frame.size.width -2+ shadowSize,
                      cell.bgView.frame.size.height+1 + shadowSize)];
    self.bgView.layer.shadowPath = shadowPath.CGPath;
}

答案 1 :(得分:0)

使用此:

  override func draw(_ rect: CGRect) {
        super.draw(rect)
        // UIBezierPath job
    }