首页 > 解决方案 > TableViewCell 中的 ProgressView 在滚动时会出现奇怪的圆角

问题描述

我有一个自定义表格视图单元格,其中有一个进度视图。起初一切看起来都很好(在这个例子中,进度条是满的):

但是当我进行一些滚动时,进度视图会像这样扭曲:

这绝对与重复使用细胞有关。我对这一切都很熟悉,是的,问题恰好在您期望的时候发生。这是我的代码;这里发生了很多事情,所以也许其中一些是在错误的地方?:

let Cell = tableView.dequeueReusableCell(withIdentifier: "GroupTableViewCell", for: indexPath) as! GroupTableViewCell
    let MyGroup = Application.Variables.SELECTED_TAB?.Groups[(indexPath as NSIndexPath).row]
    Cell.MainView.layer.cornerRadius = 4
    Cell.MainView.clipsToBounds = true
    Cell.ProgressView.layer.cornerRadius = 4
    Cell.ProgressView.clipsToBounds = true
    Cell.DescriptionLabel.text = MyGroup?.Description
    Cell.DescriptionLabel.layer.shadowRadius = 4
    Cell.DescriptionLabel.layer.shadowColor = UIColor.white.cgColor
    Cell.DescriptionLabel.layer.shadowOffset = CGSize(width: 0, height: 0)
    Cell.DescriptionLabel.layer.shadowOpacity = 1
    Cell.DescriptionLabel.layer.masksToBounds = false
    
    Cell.SubtitleLabel.layer.shadowRadius = 4
    Cell.SubtitleLabel.layer.shadowColor = UIColor.white.cgColor
    Cell.SubtitleLabel.layer.shadowOffset = CGSize(width: 0, height: 0)
    Cell.SubtitleLabel.layer.shadowOpacity = 1
    Cell.SubtitleLabel.layer.masksToBounds = false
            
    Cell.ProgressButton.titleLabel?.font = UIFont.fontAwesome(ofSize: 20, style: .regular)
    Cell.ProgressButton.tintColor = UIColor.white
    Cell.ProgressButton.layer.cornerRadius = 24
    Cell.ProgressButton.clipsToBounds = true
    Cell.ProgressButton.imageEdgeInsets = UIEdgeInsets.init(top: 12,left: 12,bottom: 12,right: 12)
    Cell.ProgressButton.layer.shadowRadius = 2.0
    Cell.ProgressButton.layer.shadowColor = UIColor.black.cgColor
    Cell.ProgressButton.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
    Cell.ProgressButton.layer.shadowOpacity = 0.40
    Cell.ProgressButton.layer.masksToBounds = false
    
    if (MyGroup?.PrescriptionStatus == Prescription.PRESCRIPTION_STATUS_WAITING_FOR_COLLECTION) {
        Cell.ProgressButton.setTitle(String.fontAwesomeIcon(name: .dollyflatbedalt), for: .normal)
        Cell.ProgressButton.backgroundColor = UIColor.pts_green
        Cell.SubtitleLabel.text = "x" + String(MyGroup!.Prescriptions.count) + " Rx waiting for collection"
    }

    if (MyGroup?.PrescriptionStatus == Prescription.PRESCRIPTION_STATUS_COLLECTED) {
        if (MyGroup!.IsReadyForUpload) {
            Cell.ProgressButton.setTitle(String.fontAwesomeIcon(name: .clouduploadalt), for: .normal)
            Cell.ProgressButton.backgroundColor = UIColor.pts_blue
        } else {
            Cell.ProgressButton.setTitle(String.fontAwesomeIcon(name: .flagcheckered), for: .normal)
            Cell.ProgressButton.backgroundColor = UIColor.pts_blue
        }
        Cell.SubtitleLabel.text = "x" + String(MyGroup!.Prescriptions.count) + " Rx collected!"
    }
    if (MyGroup?.PrescriptionStatus == Prescription.PRESCRIPTION_STATUS_WAITING_FOR_DELIVERY) {
        Cell.ProgressButton.setTitle(String.fontAwesomeIcon(name: .boxcheck), for: .normal)
        Cell.ProgressButton.backgroundColor = UIColor.pts_green
        Cell.SubtitleLabel.text = "x" + String(MyGroup!.Prescriptions.count) + " Rx waiting for delivery"
    }
    if (MyGroup?.PrescriptionStatus == Prescription.PRESCRIPTION_STATUS_DELIVERED) {
        if (MyGroup!.IsReadyForUpload) {
            Cell.ProgressButton.setTitle(String.fontAwesomeIcon(name: .clouduploadalt), for: .normal)
            Cell.ProgressButton.backgroundColor = UIColor.pts_blue
        } else {
            Cell.ProgressButton.setTitle(String.fontAwesomeIcon(name: .clouduploadalt), for: .normal)
            Cell.ProgressButton.backgroundColor = UIColor.pts_blue
        }
        Cell.SubtitleLabel.text = "x" + String(MyGroup!.Prescriptions.count) + " Rx delivered!"
    }
    
    let Max:Double = MyGroup!.EndTimeSeconds - MyGroup!.StartTimeSeconds
    let Progress:Double = Date().timeIntervalSince1970 - MyGroup!.StartTimeSeconds

    let ProgressForTextView:Int = Int(MyGroup!.EndTimeSeconds - Date().timeIntervalSince1970)
    let hours = (ProgressForTextView % 86400) / 3600
    let minutes = (ProgressForTextView % 3600) / 60
    if (hours < 0 || minutes < 0) {
        Cell.TargetDeliveryTimeLabel.isHidden = true
    } else {
        var TargetTimeString:String = ""
        if (hours <= 0) {
            TargetTimeString = ""
        }
        if (hours == 1) {
            TargetTimeString = "one hour, "
        }
        if (hours >= 2) {
            TargetTimeString = String(hours) + " hours, "
        }
        if (minutes <= 0) {
            TargetTimeString = TargetTimeString + "less than one minute"
        }
        if (minutes == 1) {
            TargetTimeString = TargetTimeString + "one minute"
        }
        if (minutes >= 2) {
            TargetTimeString = TargetTimeString + String(minutes) + " minutes"
        }
        if (TargetTimeString != "") {
            TargetTimeString = TargetTimeString + " left"
        }
        Cell.TargetDeliveryTimeLabel.text = TargetTimeString
        Cell.TargetDeliveryTimeLabel.isHidden = false
    }
    
    if (Max >= Progress) {
        Cell.DescriptionLabel.tintColor = UIColor.black
        Cell.SubtitleLabel.tintColor = UIColor.black
        Cell.TargetDeliveryTimeLabel.tintColor = UIColor.pts_darkergrey
        Cell.ProgressView.progressTintColor = UIColor.pts_pbgreen
        Cell.ProgressView.setProgress(Float(Progress / Max), animated: false)
        if (Max * 0.75 <= Progress) {
            Cell.ProgressView.progressTintColor = UIColor.pts_pbamber
        }
    } else {
        Cell.DescriptionLabel.tintColor = UIColor.white
        Cell.SubtitleLabel.tintColor = UIColor.white
        Cell.TargetDeliveryTimeLabel.tintColor = UIColor.white
        Cell.ProgressView.progressTintColor = UIColor.pts_pbred
        Cell.ProgressView.setProgress(1, animated: false)
    }
    
    return Cell

注释掉将进度条变为红色的代码似乎可以规避这个问题......但我确实需要计算代码中的颜色。进度视图的约束将 Trailing、Leading、Top 和 Bottom 空间设置为超级视图 ( MainView ),常量为 1(用于小边距)。

进度视图是否以某种方式继承了按钮的角半径?

标签: swiftxcodeuitableviewscrolluiprogressview

解决方案


使用 .tintColor 而不是 .progressTintColor 回避了这个问题。


推荐阅读