I am not able to update the data of the Header View of a CollectionView
. This header view is a subclass of UICollectionReusableView
.
I tried to call a method for reloading this data in different places but none of them is triggering the update. To see the updated data I have to close and open the app again.
Collection View:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard kind == UICollectionView.elementKindSectionHeader else {
fatalError("Unexpected element kind.")
}
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ProfileHeaderView", for: indexPath) as! ProfileHeaderView
header?.reloadData()
// headerView.userNameLabel.text = defaults.string(forKey: "username")
return headerView
}
Header View:
class ProfileHeaderView: UICollectionReusableView {
@IBOutlet weak var userNameLabel: UILabel!
func reloadData() {
let defaults = UserDefaults.standard
usernameTextLabel.text = defaults.string(forKey: "username")
}
I also tried to call reloadData()
on ViewWillAppear
and ViewDidAppear
You can try getting the header, for example:
func reloadHeaderData() {
guard let header = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: YourIndexPath) as? ProfileHeaderView else { return }
header.reloadData()
}