I have deployed 2 Supplementary Views for Header and Footer views for my collection view.
Header '0' has UILabel text which dynamically sets data dynamically after transitioning to push secondary view controller.
But I am having problem that when UILabel in header loads again after returning and taking data from secondary view controller collection view get updated but Header and Footer Supplementary views has overlapping of old and new data...!!!
How to fix overlapping of data and 'Reload Supplementary Views' for Header and Footer?
Header '0' has UILabel text which dynamically sets data dynamically after transitioning to push secondary view controller.
But I am having problem that when UILabel in header loads again after returning and taking data from secondary view controller collection view get updated but Header and Footer Supplementary views has overlapping of old and new data...!!!
How to fix overlapping of data and 'Reload Supplementary Views' for Header and Footer?
Here is the problem: line 8 you recreate a label and add it over if there is already an existing one:
You should either:
And call in your case:
And now you can safely add subview:
Code Block let headerUILabelText = UILabel() headerUILabelText.textAlignment = .left headerUILabelText.font = UIFont.systemFont(ofSize: 20, weight: .bold) headerUILabelText.numberOfLines = 0 headerUILabelText.text = "Your Roll Number is \(NumberInteger). Your Exam Numbers are \(examNumbers(number: "\(NumberInteger)"))" headerUILabelText.textColor = .darkGray headerUILabelText.lineBreakMode = .byTruncatingTail headerView.addSubview(headerUILabelText)
You should either:
remove subviews first
test if there is already a UILabel and not recreate if so
replace existing subview.
Code Block extension UIView { class func getAllSubviews<T: UIView>(from parentView: UIView) -> [T] { return parentView.subviews.flatMap { subView -> [T] in var result = getAllSubviews(from: subView) as [T] if let view = subView as? T { result.append(view) } return result } } class func getAllSubviews(from parentView: UIView, types: [UIView.Type]) -> [UIView] { return parentView.subviews.flatMap { subView -> [UIView] in var result = getAllSubviews(from: subView) as [UIView] for type in types { if subView.classForCoder == type { result.append(subView) return result } } return result } } func getAllSubviews<T: UIView>() -> [T] { return UIView.getAllSubviews(from: self) as [T] } func get<T: UIView>(all type: T.Type) -> [T] { return UIView.getAllSubviews(from: self) as [T] } func get(all type: UIView.Type) -> [UIView] { return UIView.getAllSubviews(from: self) } }
And call in your case:
Code Block let allLabels = self.headerView.get(all: UILabel.self) for sub in allLabels { sub.removeFromSuperview() }
And now you can safely add subview:
Code Block headerView.addSubview(headerUILabelText)