UITableView分区周围的边框

22

如何为每个表视图节添加边框?以下是所需效果的示意图。如果您查看图片,它在每个表视图节周围都有一个边框。

图片描述


请查看此链接:http://stackoverflow.com/questions/9773308/uitableview-section-border?rq=1 - Baby Groot
@HinataHyuga - 我已经检查过了 - 它将边框放在整个表格周围,而不是每个部分。 - itsaboutcode
@itsaboutcode 你找到解决方案了吗? - aqavi_paracha
8个回答

26

针对此问题,我根据 jvanmetre 的答案进行了改编,以使表视图的角变得更加圆润。只需添加以下代码的 tableView:willDisplayCell:forRowAtIndexPath: 代理方法(简单地复制/粘贴即可),它也适用于分组表格。我已经在注释中指出了您需要设置边框宽度和颜色的位置。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {

    if ([cell respondsToSelector:@selector(tintColor)]) {
        CGFloat cornerRadius = 5.f;
        cell.backgroundColor = UIColor.clearColor;
        CAShapeLayer *layer = [[CAShapeLayer alloc] init];
        CGMutablePathRef pathRef = CGPathCreateMutable();
        CGRect bounds = CGRectInset(cell.bounds, 10, 0);
        BOOL addLine = NO;
        if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
            CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
        } else if (indexPath.row == 0) {
            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
            addLine = YES;
        } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
        } else {
            CGPathAddRect(pathRef, nil, bounds);
            addLine = YES;
        }
        layer.path = pathRef;
        CFRelease(pathRef);
        //set the border color
        layer.strokeColor = [UIColor lightGrayColor].CGColor;
        //set the border width
        layer.lineWidth = 1;
        layer.fillColor = [UIColor colorWithWhite:1.f alpha:1.0f].CGColor;


        if (addLine == YES) {
            CALayer *lineLayer = [[CALayer alloc] init];
            CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
            lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight);
            lineLayer.backgroundColor = tableView.separatorColor.CGColor;
            [layer addSublayer:lineLayer];
        }

        UIView *testView = [[UIView alloc] initWithFrame:bounds];
        [testView.layer insertSublayer:layer atIndex:0];
        testView.backgroundColor = UIColor.clearColor;
        cell.backgroundView = testView;
    }
}

同时,请记得在Interface Builder中将表格的分隔符属性设置为none(默认为单行),如果您正在以编程方式创建表格,则应像这样设置属性

yourTableView.separatorStyle = UITableViewCellSeparatorStyleNone

这非常好!如果您想要更改背景颜色,请将 layer.fillColor 更改为所需的任何颜色。 - green0range
这不会涵盖headerView。 - Mutawe
如果超过2行就无法工作:问题在最后一个else语句中——它会生成一个矩形边框,即4条边界线。 - John Smith
如果我给边框添加了阴影,那么每个单元格后面会出现一个分隔符,如何消除这个问题? - Shourob Datta
如何给文本添加阴影?有人可以帮忙吗? - Shangari C

18

这是Asad的回答,已更新至Swift 3

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if (cell.responds(to: #selector(getter: UIView.tintColor))) {
        let cornerRadius: CGFloat = 5
        cell.backgroundColor = UIColor.clear
        let layer: CAShapeLayer  = CAShapeLayer()
        let pathRef: CGMutablePath  = CGMutablePath()
        let bounds: CGRect  = cell.bounds.insetBy(dx: 10, dy: 0)
        var addLine: Bool  = false
        if (indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section)-1) {
            pathRef.__addRoundedRect(transform: nil, rect: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)
        } else if (indexPath.row == 0) {
            pathRef.move(to: CGPoint(x:bounds.minX,y:bounds.maxY))
            pathRef.addArc(tangent1End: CGPoint(x:bounds.minX,y:bounds.minY), tangent2End: CGPoint(x:bounds.midX,y:bounds.minY), radius: cornerRadius)

            pathRef.addArc(tangent1End: CGPoint(x:bounds.maxX,y:bounds.minY), tangent2End: CGPoint(x:bounds.maxX,y:bounds.midY), radius: cornerRadius)
            pathRef.addLine(to: CGPoint(x:bounds.maxX,y:bounds.maxY))
            addLine = true;
        } else if (indexPath.row == tableView.numberOfRows(inSection: indexPath.section)-1) {

            pathRef.move(to: CGPoint(x:bounds.minX,y:bounds.minY))
            pathRef.addArc(tangent1End: CGPoint(x:bounds.minX,y:bounds.maxY), tangent2End: CGPoint(x:bounds.midX,y:bounds.maxY), radius: cornerRadius)

            pathRef.addArc(tangent1End: CGPoint(x:bounds.maxX,y:bounds.maxY), tangent2End: CGPoint(x:bounds.maxX,y:bounds.midY), radius: cornerRadius)
            pathRef.addLine(to: CGPoint(x:bounds.maxX,y:bounds.minY))

        } else {
            pathRef.addRect(bounds)
            addLine = true
        }
        layer.path = pathRef
        //CFRelease(pathRef)
        //set the border color
        layer.strokeColor = UIColor.lightGray.cgColor;
        //set the border width
        layer.lineWidth = 1
        layer.fillColor = UIColor(white: 1, alpha: 1.0).cgColor


        if (addLine == true) {
            let lineLayer: CALayer = CALayer()
            let lineHeight: CGFloat  = (1 / UIScreen.main.scale)
            lineLayer.frame = CGRect(x:bounds.minX, y:bounds.size.height-lineHeight, width:bounds.size.width, height:lineHeight)
            lineLayer.backgroundColor = tableView.separatorColor!.cgColor
            layer.addSublayer(lineLayer)
        }

        let testView: UIView = UIView(frame:bounds)
        testView.layer.insertSublayer(layer, at: 0)
        testView.backgroundColor = UIColor.clear
        cell.backgroundView = testView
    }

}

如果超过2行就无法工作:问题在最后一个else语句中——它会生成一个矩形边框,即4条边界线。 - John Smith
如何给文本添加阴影?有人可以帮忙吗? - Shangari C

2
今日免费次数已满, 请开通会员/明日再来
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if (cell.respondsToSelector("tintColor")) {
        var cornerRadius: CGFloat = 5;
        cell.backgroundColor = UIColor.clearColor()
        var layer: CAShapeLayer  = CAShapeLayer()
        var pathRef: CGMutablePathRef  = CGPathCreateMutable()
        var bounds: CGRect  = CGRectInset(cell.bounds, 10, 0)
        var addLine: Bool  = false
        if (indexPath.row == 0 && indexPath.row == tableView.numberOfRowsInSection(indexPath.section)-1) {
            CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
        } else if (indexPath.row == 0) {
            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
            addLine = true;
        } else if (indexPath.row == tableView.numberOfRowsInSection(indexPath.section)-1) {
            CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
            CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
            CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
        } else {
            CGPathAddRect(pathRef, nil, bounds);
            addLine = true;
        }
        layer.path = pathRef;
        //CFRelease(pathRef);
        //set the border color
        layer.strokeColor = UIColor.lightGrayColor().CGColor;
        //set the border width
        layer.lineWidth = 1;
        layer.fillColor = UIColor(white: 1, alpha: 1.0).CGColor;


        if (addLine == true) {
            var lineLayer: CALayer = CALayer();
            var lineHeight: CGFloat  = (1 / UIScreen.mainScreen().scale);
            lineLayer.frame = CGRectMake(CGRectGetMinX(bounds), bounds.size.height-lineHeight, bounds.size.width, lineHeight);
            lineLayer.backgroundColor = tableView.separatorColor!.CGColor;
            layer.addSublayer(lineLayer);
        }

        var testView: UIView = UIView(frame:bounds)
        testView.layer.insertSublayer(layer, atIndex: 0)
        testView.backgroundColor = UIColor.clearColor()
        cell.backgroundView = testView
    }

}

如果超过两行就不起作用:问题在最后一个else语句中——它会生成一个矩形边框,即4条边界线。 - John Smith
如何为我所写的元素添加阴影?可以有人帮帮我吗? - Shangari C

2

我有另一种方法,可能对某些人有帮助。它使用预定义的颜色和宽度为每个区域绘制边框,而不改变单元格的其他属性。它不能给你一个圆形的区域(可能可以相应地修改),但它可以很好地控制要绘制的线条。这种方法还解决了设备旋转的问题。

typedef enum CellBorderMask{
    CellBorderMaskLeft      = 1 << 0,
    CellBorderMaskRigth     = 1 << 1,
    CellBorderMaskTop       = 1 << 2,
    CellBorderMaskBottom    = 1 << 3
}CellBorderMask;




- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    CellBorderMask mask;
    if (indexPath.row == 0){
        mask |= CellBorderMaskTop;
    }

    if(indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) {
        mask |= CellBorderMaskBottom;
    }
    mask |= CellBorderMaskRigth | CellBorderMaskLeft;
    [self addBorder:mask forView:cell.contentView];
}

-(void)addBorder:(CellBorderMask)mask forView:(UIView *)view{
    float onePixel = (1.f / [UIScreen mainScreen].scale);
    float lineWidth = 1 * onePixel;
    CGColorRef cgBorderColor = [UIColor redColor].CGColor;


    CALayer *topBorder = [CALayer layer];
    CALayer *bottomBorder = [CALayer layer];
    CALayer *leftBorder = [CALayer layer];
    CALayer *rightBorder = [CALayer layer];

    //tag layers so it's possible to find and remove them later 
    topBorder.name = @"Border";
    bottomBorder.name = @"Border";
    leftBorder.name = @"Border";
    rightBorder.name = @"Border";

    //remove previously set border layers so they doesn't produce unwanted effect on orientation change
    [self cleanUpOldBorderLayers:view];

    topBorder.frame = CGRectMake(0.0f, 0.0f, view.bounds.size.width, lineWidth);
    topBorder.backgroundColor = cgBorderColor;

    bottomBorder.frame = CGRectMake(0.0f, view.bounds.size.height - lineWidth, view.bounds.size.width, lineWidth);
    bottomBorder.backgroundColor = cgBorderColor;

    leftBorder.frame = CGRectMake(0.0f, 0.0f, lineWidth, view.bounds.size.height);
    leftBorder.backgroundColor = cgBorderColor;

    rightBorder.frame = CGRectMake(view.bounds.size.width - lineWidth, 0.0f, lineWidth, view.bounds.size.height);
    rightBorder.backgroundColor = cgBorderColor;
    if(mask & CellBorderMaskTop){
        [view.layer addSublayer:topBorder];
    }
    if(mask & CellBorderMaskBottom){
        [view.layer addSublayer:bottomBorder];
    }
    if(mask & CellBorderMaskLeft){
        [view.layer addSublayer:leftBorder];
    }
    if(mask & CellBorderMaskRigth){
        [view.layer addSublayer:rightBorder];
    }
}

-(void)cleanUpOldBorderLayers:(UIView *)view{
    NSMutableArray *layerArray = [NSMutableArray new];
    for (CALayer *layer in view.layer.sublayers) {
        if([@"Border" isEqualToString:layer.name]){
            [layerArray addObject:layer];
        }
    }
    for (CALayer *layer in layerArray) {
        [layer removeFromSuperlayer];
    }
}


-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    //need to trigger tableView:willDisplayCell: method on orientation change. 
    //Suggest a better method for this if there is one
    [self.tableView reloadData];
}

我使用tableView的reloadData方法重新绘制单元格,但我认为这不是最好的方法。请在评论中提供替代方法,我将更新代码。

要为标题/页脚添加边框,只需在viewForHeaderInSection/viewForFooterInSection委托方法中创建自定义视图,并将该视图传递给上面的addBorder:forView方法,然后从您的委托返回它。


1

解决不同部分中行数不同的工作方法

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            let cornerRadius: CGFloat = 0.0
            cell.backgroundColor = UIColor.clear
            let layer: CAShapeLayer = CAShapeLayer()
            let pathRef: CGMutablePath = CGMutablePath()
            //dx leading an trailing margins
            let bounds: CGRect = cell.bounds.insetBy(dx: 8, dy: 0)
            var addLine: Bool = false

            if indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
                pathRef.__addRoundedRect(transform: nil, rect: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)
            } else if indexPath.row == 0 {
                pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
                pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.minY),
                               tangent2End: CGPoint(x: bounds.midX, y: bounds.minY),
                               radius: cornerRadius)

                pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.minY),
                               tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY),
                               radius: cornerRadius)
                pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
                addLine = true
            } else if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
                pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
                pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY),
                               tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY),
                               radius: cornerRadius)

                pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY),
                               tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY),
                               radius: cornerRadius)
                pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
            } else {
                pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
                pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY),
                               tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY),
                               radius: cornerRadius)

                pathRef.move(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
                pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY),
                               tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY),
                               radius: cornerRadius)
                pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
                addLine = true
            }

            layer.path = pathRef
            layer.strokeColor = UIColor.lightGray.cgColor
            layer.lineWidth = 1.0
            layer.fillColor = UIColor.clear.cgColor

            if addLine == true {
                let lineLayer: CALayer = CALayer()
                let lineHeight: CGFloat = (1 / UIScreen.main.scale)
                lineLayer.frame = CGRect(x: bounds.minX, y: bounds.size.height - lineHeight, width: bounds.size.width, height: lineHeight)
                lineLayer.backgroundColor = UIColor.clear.cgColor
                layer.addSublayer(lineLayer)
            }

            let backgroundView: UIView = UIView(frame: bounds)
            backgroundView.layer.insertSublayer(layer, at: 0)
            backgroundView.backgroundColor = .clear
            cell.backgroundView = backgroundView

        }

0

这是答案的Swift3版本:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let cornerRadius: CGFloat = 0.0
    cell.backgroundColor = UIColor.clear
    let layer: CAShapeLayer = CAShapeLayer()
    let pathRef: CGMutablePath = CGMutablePath()
    let bounds: CGRect = cell.bounds.insetBy(dx: 10, dy: 0)
    var addLine: Bool = false

    if indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
        pathRef.__addRoundedRect(transform: nil, rect: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)
    } else if indexPath.row == 0 {
        pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
        pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.minY), tangent2End: CGPoint(x: bounds.midX, y: bounds.minY), radius: cornerRadius)
        pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.minY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
        pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
        addLine = true
    } else if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
        pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
        pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY), radius: cornerRadius)
        pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
        pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
    } else {
        pathRef.addRect(bounds)
        addLine = true
    }

    layer.path = pathRef
    layer.strokeColor = UIColor.black.cgColor
    layer.lineWidth = 0.5
    layer.fillColor = UIColor(white: 1, alpha: 1.0).cgColor

    if addLine == true {
        let lineLayer: CALayer = CALayer()
        let lineHeight: CGFloat = (1 / UIScreen.main.scale)
        lineLayer.frame = CGRect(x: bounds.minX, y: bounds.size.height - lineHeight, width: bounds.size.width, height: lineHeight)
        lineLayer.backgroundColor = tableView.separatorColor!.cgColor
        layer.addSublayer(lineLayer)
    }

    let backgroundView: UIView = UIView(frame: bounds)
    backgroundView.layer.insertSublayer(layer, at: 0)
    backgroundView.backgroundColor = UIColor.clear
    cell.backgroundView = backgroundView
}

0

这个答案解决了一个问题,即在某个区域中给定的半径具有超过2个单元格。

半径:cornerRadius -> 0.0

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let cornerRadius: CGFloat = 4.0
        cell.backgroundColor = UIColor.clear
        let layer: CAShapeLayer = CAShapeLayer()
        let pathRef: CGMutablePath = CGMutablePath()
        //dx leading an trailing margins
        let bounds: CGRect = cell.bounds.insetBy(dx: 15, dy: 0)
        var addLine: Bool = false
        
        if indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
            pathRef.__addRoundedRect(transform: nil, rect: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)
        } else if indexPath.row == 0 {
            pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
            pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.minY),
                           tangent2End: CGPoint(x: bounds.midX, y: bounds.minY),
                           radius: cornerRadius)
            
            pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.minY),
                           tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY),
                           radius: cornerRadius)
            pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
            addLine = true
        } else if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
            pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
            pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY),
                           tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY),
                           radius: cornerRadius)
            
            pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY),
                           tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY),
                           radius: cornerRadius)
            pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
        } else {
            pathRef.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
            pathRef.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY),
                           tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY),
                           radius: 0.0)
            
            pathRef.move(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
            pathRef.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY),
                           tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY),
                           radius: 0.0)
            pathRef.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
            addLine = true
        }
        
        layer.path = pathRef
        layer.strokeColor = UIColor(fromHexString: "#D8D8D8")?.cgColor
        layer.lineWidth = 1.0
        layer.fillColor = UIColor.clear.cgColor
        
        if addLine == true {
            let lineLayer: CALayer = CALayer()
            let lineHeight: CGFloat = (1 / UIScreen.main.scale)
            lineLayer.frame = CGRect(x: bounds.minX, y: bounds.size.height - lineHeight, width: bounds.size.width, height: lineHeight)
            lineLayer.backgroundColor = UIColor.clear.cgColor
            layer.addSublayer(lineLayer)
        }
        
        let backgroundView: UIView = UIView(frame: bounds)
        backgroundView.layer.insertSublayer(layer, at: 0)
        backgroundView.backgroundColor = .clear
        cell.backgroundView = backgroundView
        
    }

-1

我修改了上面发布的代码,现在这段代码可以为部分创建边框。 已在Swift4.2中测试。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let cornerRadius: CGFloat = 24
        cell.backgroundColor = .clear

        let layer = CAShapeLayer()
        let pathRef = CGMutablePath()
        let bounds = cell.bounds.insetBy(dx: 10, dy: 0)
        var addLine = false

        if indexPath.row == 0 && indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
            pathRef.__addRoundedRect(transform: nil, rect: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)
        } else if indexPath.row == 0 {
            pathRef.move(to: .init(x: bounds.minX, y: bounds.maxY))
            pathRef.addArc(tangent1End: .init(x: bounds.minX, y: bounds.minY), tangent2End: .init(x: bounds.midX, y: bounds.minY), radius: cornerRadius)
            pathRef.addArc(tangent1End: .init(x: bounds.maxX, y: bounds.minY), tangent2End: .init(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
            pathRef.addLine(to: .init(x: bounds.maxX, y: bounds.maxY))
            addLine = true
        } else if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
            pathRef.move(to: .init(x: bounds.minX, y: bounds.minY))
            pathRef.addArc(tangent1End: .init(x: bounds.minX, y: bounds.maxY), tangent2End: .init(x: bounds.midX, y: bounds.maxY), radius: cornerRadius)
            pathRef.addArc(tangent1End: .init(x: bounds.maxX, y: bounds.maxY), tangent2End: .init(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
            pathRef.addLine(to: .init(x: bounds.maxX, y: bounds.minY))
        } else {
           //pathRef.addRect(bounds)

            let pathInnerRef = CGMutablePath()
            pathInnerRef.move(to: .init(x: bounds.minX, y: bounds.minY))
            pathInnerRef.addLine(to: .init(x: bounds.minX, y: bounds.maxY))

            pathInnerRef.move(to: .init(x: bounds.maxX, y: bounds.minY))
            pathInnerRef.addLine(to: .init(x: bounds.maxX, y: bounds.maxY))
            pathRef.addPath(pathInnerRef)
            addLine = true
        }

        layer.path = pathRef
        layer.fillColor = UIColor.clear.cgColor
        layer.strokeColor = UIColor.black.cgColor


        if (addLine == true) {
            let lineLayer = CALayer()
            let lineHeight = 1.0 / UIScreen.main.scale
            lineLayer.frame = CGRect(x: bounds.minX + 10, y: bounds.size.height - lineHeight, width: bounds.size.width - 10, height: lineHeight)
            lineLayer.backgroundColor = UIColor.clear.cgColor//tableView.separatorColor?.cgColor
            lineLayer.addBorder(edge: .left, color: UIColor.black, thickness: 2.0)
            lineLayer.addBorder(edge: .right, color: UIColor.black, thickness: 2.0)
            layer.addSublayer(lineLayer)
        }


        let testView = UIView(frame: bounds)
        testView.layer.insertSublayer(layer, at: 0)
        testView.backgroundColor = .clear

        cell.backgroundView = testView   
}

如果该部分有标题,则该解决方案无法正常工作。 - skri
如何为此卡片添加阴影。如果我应用了顶部和底部,由于“dy:0”,它们并不会出现。 - Shangari C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接