UITableViewCell内嵌UIProgressView滚动复用时出现圆角扭曲问题
内嵌UIProgressView的自定义UITableViewCell滚动后扭曲问题
问题现象
我开发了一款内嵌UIProgressView的自定义UITableViewCell,页面初始加载时(示例中进度条为满值)显示效果完全正常:
但滚动列表后,进度条会出现扭曲变形:
经初步判断,问题与UITableView的单元格复用机制直接相关,触发时机完全符合单元格复用的预期场景。
问题相关实现代码
以下为tableView(_:cellForRowAt:)代理方法中的核心实现,由于代码逻辑较多,暂不确定部分实现是否放置在正确位置:
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
排查进展与待验证疑问
- 经测试,注释掉将进度条设置为红色的逻辑可暂时规避该问题,但业务场景需要在代码中动态计算进度条颜色,临时注释的方案无法长期使用。
- 当前UIProgressView的Auto Layout约束设置为距离父视图MainView的Leading、Trailing、Top、Bottom间距均为1pt,预留了微小内边距。
- 待验证疑问:是否UIProgressView会异常继承同单元格内按钮的圆角属性,最终导致显示异常?
内容的提问来源于stack exchange,提问作者Psiloc
相关产品推荐
相关产品推荐

