ios - 滚动表格 View 时,标签的角半径变得扭曲

标签 ios swift uitableview

我的 View 中有一个标签,根据某些条件,该标签对于某些行将可见。我在堆栈 View 中添加了标签。标签的 UI 是背景色和圆角半径。

我有子类 UItableview 单元格并使用了以下代码:

class SampleCell: UITableViewCell {
    @IBOutlet weak var status: UILabel!
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        self.status.text = nil
    }

    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        layoutIfNeeded()
    }

    func setUpStatusLabel(isShow: Bool) {
        self.status.text = "Status"

        self.status.textColor = UIColor.red
        self.status.font = UIFont.systemFont(ofSize: 11)
        self.status.layer.cornerRadius = 
        self.status.frame.size.height/2
        self.status.layer.masksToBounds = false
        self.status.layer.backgroundColor = UIColor.lightGray.cgColor
        self.status.isHidden = !isShow
    }
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = (tableView.dequeueReusableCell(withIdentifier: "sample", for: indexPath) as? SampleCell)!
if indexPath.row % 2 == 0 {
            cell.setUpStatusLabel(isShow: true)
        } else {
            cell.setUpStatusLabel(isShow: false)
        }
}

当我启动此代码并滚动表格 View 时,标签的角半径有时会以这样的方式显示矩形 View ,这意味着没有角半径,有时背景颜色也会丢失。当用户滚动表格 View 时,我需要保留所有这些。

最佳答案

您需要为 isShow 的两种情况定义您的 UI:

func setUpStatusLabel(isShow: Bool) {
    self.status.isHidden = !isShow
    self.status.layer.masksToBounds = false
    self.status.layer.backgroundColor = UIColor.lightGray.cgColor
    self.status.font = UIFont.systemFont(ofSize: 11)

    if isShow {
        self.status.text = "Status"
        self.status.textColor = UIColor.red
        self.status.layer.cornerRadius = self.status.frame.size.height/2
    } else {
        //change the following according to your need.

        self.status.text = ""
        self.status.textColor = UIColor.darkText
        self.status.layer.cornerRadius = 0
    }
}

关于ios - 滚动表格 View 时,标签的角半径变得扭曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58165051/

相关文章:

iphone - iOS 帮助 : Custom design for UITableViewCell class is not showing up in UITableView

ios - 运行 Xcode UI 测试时如何禁用自动完成?

swift - 在 Swift 中更改字典的键

ios - 带有自定义 UITableViewCell 的 UITableView 中的 UITextField 中的 UIDatePicker : realtime update

ios - 在 XIB 中使用多个 UITableView

ios - 将 CGPath 点转换为不同的 UIView 引用

ios - 用户uid错误! (Swift 和 Firebase 项目)

ios - 如何使用来自 webservice 的字符串数组到 INSPhotoViewable

swift - 如何从完成 block 返回值?

ios - 从单独的类输出二维数组中的元素