我正在尝试使用角半径来查看放在UITableView中的内容。它对我不起作用

问题描述 投票:1回答:1

我试图给UIView提供角半径,UIView放在UITableViewCell中。我需要为每个部分显示像角半径的部分,所以我给第一个单元格到右上角和左上角半径,最后一个单元格给出右下角和左下角半径。但这对我不起作用。

这是我的代码:这是UITableView的第一个单元格(行)

cell.viewMain.roundCorners(corners: [.topLeft, .topRight], radius: 10.0)
cell.layoutSubviews()
cell.layoutIfNeeded()

这是UITableView的最后一个单元格(行)

cell.viewMain.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10.0)
cell.layoutSubviews()
cell.layoutIfNeeded()

我正在使用的扩展:

extension UIView {
    func roundCorners(corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        layer.mask = mask
    }
}

我需要像这样的输出:

enter image description here

ios swift uitableview uiview rounded-corners
1个回答
1
投票

我有一个类似的问题,我失踪的是表视图重用单元格,所以我们必须将所有其他单元格还原为默认状态。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! myCell

    let isFirstCell = indexPath.row == 0
    let isLastCell = indexPath.row == TotalRowsCount - 1

    if isFirstCell && isLastCell {
        cell.viewMain.topBottomRounded()
    } else if isFirstCell {
        cell.viewMain.topRounded()
    } else if isLastCell {
        cell.viewMain.bottomRounded()
    } else {
        // THIS IS THE KEY THING
        cell.viewMain.defaultStateForBorders()
    }

    return cell
}

我已经运行了你的代码,它的工作正常,除了这件事。

使用扩展方法帮助方法的地方是:

extension UIView {

func roundCorners(corners: UIRectCorner, radius: CGFloat) {
    let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
    let mask = CAShapeLayer()
    mask.path = path.cgPath
    layer.mask = mask
}

func topRounded() {
    self.roundCorners(corners: [.topLeft, .topRight], radius: 10.0)
}

func bottomRounded() {
    self.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10.0)
}

func topBottomRounded() {
    self.roundCorners(corners: [.topLeft, .topRight,.bottomLeft, .bottomRight], radius: 10.0)
}

func defaultStateForBorders() {
    self.roundCorners(corners: [], radius: 0)
}

}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.