TableViewCell 内的 ProgressView 在滚动时出现奇怪的圆角 [英] ProgressView inside TableViewCell gets weird rounded corners on scroll
问题描述
我有一个自定义的表格视图单元格,其中有一个进度视图.一开始一切看起来都很好(在这个例子中进度条是满的):
I have a custom Table View Cell that has a progress view in it. Everything looks fine at first (in this example the progress bar is full):
但是当我进行一些滚动时,进度视图会像这样扭曲:
But when I do some scrolling, the progress view distorts like this:
这肯定与重复使用单元格有关.我对这一切都很熟悉,是的,问题恰好在您期望的时候发生.这是我的代码;这里发生了很多事情,所以也许其中一些放错了地方?:
It is definitely something to do with reusing cells. I'm familiar with all that and yes the issue occurs exactly when you'd expect it to. Here is my code; there is quite a lot going on here so perhaps some of this is in the wrong place?:
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
注释掉将进度条变为红色的代码似乎可以绕过这个问题……但我确实需要计算代码中的颜色.进度视图的约束将尾随、前导、顶部和底部空间设置为超级视图 (MainView),常量为 1(用于小边距).
Commenting out the code that turns the progress bar red seems to circumvent the issue... but I do need to calculate the colour in code. The constraints for the progress view set the Trailing, Leading, Top and Bottom spaces to the superview (MainView) with a constant of 1 (for a small margin).
进度视图是否以某种方式继承了按钮的圆角半径?
Is the progress view inheriting the corner radius of the button somehow?
推荐答案
使用 .tintColor 而不是 .progressTintColor 回避了这个问题.
Using .tintColor instead of .progressTintColor sidestepped this issue.
这篇关于TableViewCell 内的 ProgressView 在滚动时出现奇怪的圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!