【发布时间】:2020-01-15 22:02:17
【问题描述】:
我无法更新CollectionView 的标题视图的数据。此标题视图是UICollectionReusableView 的子类。
我试图调用一种方法在不同的地方重新加载这些数据,但它们都没有触发更新。要查看更新的数据,我必须关闭并再次打开应用程序。
集合视图:
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
}
标题视图:
class ProfileHeaderView: UICollectionReusableView {
@IBOutlet weak var userNameLabel: UILabel!
func reloadData() {
let defaults = UserDefaults.standard
usernameTextLabel.text = defaults.string(forKey: "username")
}
我还尝试在ViewWillAppear 和ViewDidAppear 上致电reloadData()
【问题讨论】:
标签: ios swift uicollectionview reload uicollectionreusableview