首页 > 解决方案 > 在滚动表格视图时,标签的角半径变得扭曲

问题描述

我有一个标签,根据某些条件,某些行可以看到该标签。我在堆栈视图中添加了标签。标签的 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)
        }
}

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

标签: iosswiftuitableview

解决方案


在 cellForRowAt indexPath cell.selectionStyle = .none 试试这个


推荐阅读