我试图点击确实选择项目然后呈现一个ui集合视图控制器,用户是一个索引路径,我刚刚点击了谁,虽然屏幕上没有任何东西加载?
class HomePage: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var profilePage: ProfilePage?
var profilePageHeader: ProfilePageHeader?
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let user = users[indexPath.item]
let PP = ProfilePage(collectionViewLayout: UICollectionViewFlowLayout())
self.profilePageHeader?.currentUser = user
self.profilePage?.user = user
navigationController?.pushViewController(PP, animated: true)
}
}
class ProfilePage: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var user: User2?{
didSet{
let displayName = user?.DisplayName
navigationItem.title = displayName
collectionView?.reloadData()
}
}
}
答案 0 :(得分:1)
问题出现是因为在您调用collectionView?.reloadData()
我做的通常是在设置数据时调用binding
函数,在viewDidLoad
上调用
class ProfilePage: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var user: User2?{
didSet{
bind()
}
}
func bind(){
navigationItem.title = user?.DisplayName
collectionView?.reloadData()
}
override viewDidLoad(...) {
...
bind()
}
}
这样,只要数据准备好,并且视图准备就绪,就会调用绑定。第一次调用肯定是失败,因为此时数据或视图尚未就绪,但第二次调用将成功,因为此时数据和视图都将准备就绪,