gpt4 book ai didi

ios - 我正在尝试查看放置在 UITableView 中的角半径。它对我不起作用

转载 作者:可可西里 更新时间:2023-11-01 01:49:38 25 4
gpt4 key购买 nike

我正在尝试为放置在 UITableViewCell 中的 UIView 提供圆角半径。我需要为每个部分显示像角半径这样的部分,所以我将第一个单元格指定为右上角和左上角半径,最后一个单元格指定为右下角和左下角半径。但这对我不起作用。

这是我的代码:这是针对 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

最佳答案

前一段时间我遇到了类似的问题,我遗漏的是 TableView 正在重用单元格,因此我们必须将所有其他单元格恢复为默认状态。

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

关于ios - 我正在尝试查看放置在 UITableView 中的角半径。它对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55552837/

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