Swift 是否在索引路径上选择了用户,然后向ui集合视图控制器显示了用户详细信息
我试图点击did select项,然后向用户显示一个ui集合视图控制器,显示我刚才点击的用户的索引路径,尽管屏幕上没有加载任何内容Swift 是否在索引路径上选择了用户,然后向ui集合视图控制器显示了用户详细信息,swift,xcode,Swift,Xcode,我试图点击did select项,然后向用户显示一个ui集合视图控制器,显示我刚才点击的用户的索引路径,尽管屏幕上没有加载任何内容 class HomePage: UICollectionViewController, UICollectionViewDelegateFlowLayout { var profilePage: ProfilePage? var profilePageHeader: ProfilePageHeader? override func c
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()
}
}
}
出现此问题的原因是在调用
collectionView?时未加载collectionView。reloadData()
我通常做的事情是在设置数据时调用binding
函数,然后在viewDidLoad
上调用类似的函数
class ProfilePage: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var user: User2?{
didSet{
bind()
}
}
func bind(){
navigationItem.title = user?.DisplayName
collectionView?.reloadData()
}
override viewDidLoad(...) {
...
bind()
}
}
这样,只要数据准备就绪,视图准备就绪,就会调用绑定。第一次调用肯定失败,因为此时数据或视图尚未就绪,但第二次调用将成功,因为此时数据和视图都已就绪,因为在调用
collectionView?时未加载collectionView。重新加载数据()
。是否仍存在任何问题?你试过我的答案了吗?