TableViewCell 中的 ProgressView 在滚动时出现奇怪的圆角
ProgressView inside TableViewCell gets weird rounded corners on scroll
我有一个自定义 Table View Cell,其中有一个进度视图。一开始一切看起来都很好(在此示例中进度条已满):
但是当我滚动时,进度视图像这样扭曲:
肯定是和细胞复用有关。我对所有这些都很熟悉,是的,问题恰好在您期望的时候发生。这是我的代码;这里发生了很多事情所以也许其中一些是在错误的地方?:
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 空间设置为 superview (MainView),常数为 1(对于小边距)。
进度视图是否继承了按钮的圆角半径?
使用 .tintColor 而不是 .progressTintColor 回避了这个问题。
我有一个自定义 Table View Cell,其中有一个进度视图。一开始一切看起来都很好(在此示例中进度条已满):
但是当我滚动时,进度视图像这样扭曲:
肯定是和细胞复用有关。我对所有这些都很熟悉,是的,问题恰好在您期望的时候发生。这是我的代码;这里发生了很多事情所以也许其中一些是在错误的地方?:
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 空间设置为 superview (MainView),常数为 1(对于小边距)。
进度视图是否继承了按钮的圆角半径?
使用 .tintColor 而不是 .progressTintColor 回避了这个问题。