我有一个搜索控制器,当单击该搜索控制器时,将显示一个collectionViewController,其外观会有所不同,具体取决于您是登录用户还是我搜索过的用户。这是处理向用户展示的代码
weak var searchProfileViewController: SearchProfileeViewController?
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// searchBar.isHidden = true
// searchBar.resignFirstResponder()
switch scopeIndex {
case 0:
let event = filteredEvents[indexPath.item]
currentEventDetailController.eventKey = event.key!
currentEventDetailController.eventPromo = event.currentEventPromo!
currentEventDetailController.currentEvent = event
self.filteredEvents.removeAll()
self.eventsArray.removeAll()
self.collectionView?.reloadData()
present(currentEventDetailController, animated: true, completion: nil)
// navigationController?.pushViewController(currentEventDetailController, animated: true)
navigationController?.navigationBar.isHidden = false
break
case 1:
//change needs to be made here
navigationController?.navigationBar.isHidden = false
let user = filteredUsers[indexPath.item]
// print(user.username ?? "")
userProfileController.user = user
userProfileController.navigationItem.title = user.username
userProfileController.navigationItem.hidesBackButton = true
let backButton = UIBarButtonItem(image: UIImage(named: "icons8-Back-64"), style: .plain, target: self, action: #selector(GoBack))
userProfileController.navigationItem.leftBarButtonItem = backButton
var navController = UINavigationController(rootViewController: userProfileController)
present(navController, animated: true, completion: nil)
break
default:
break
}
}
现在,当输入用户控制器时,将使用以下代码配置标题,其中有两种情况
1.如果您是当前登录用户
2.如果您是我搜索过的用户
fileprivate func setupUserInteraction (){
guard let currentLoggedInUser = Auth.auth().currentUser?.uid else{
return
}
guard let uid = user?.uid else{
return
}
if currentLoggedInUser == uid {
let userStackView = UIStackView(arrangedSubviews: [profileeSettings, settings])
userStackView.distribution = .fillEqually
userStackView.axis = .vertical
userStackView.spacing = 10.0
addSubview(userStackView)
userStackView.anchor(top: topAnchor, left: leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 15, paddingBottom: 0, paddingRight: 0, width: 0, height: 90)
let bottomDividerView = UIView()
bottomDividerView.backgroundColor = UIColor.lightGray
addSubview(bottomDividerView)
bottomDividerView.anchor(top: profileStackView.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 15, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0.5)
} else{
addSubview(followButton)
followButton.anchor(top: profileStackView.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 10, paddingLeft: 50, paddingBottom:0 , paddingRight: 50, width: 0, height: 0)
let bottomDividerView = UIView()
bottomDividerView.backgroundColor = UIColor.lightGray
addSubview(bottomDividerView)
bottomDividerView.anchor(top: followButton.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 15, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0.5)
// check if following
Database.database().reference().child("following").child(currentLoggedInUser).child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
if let isFollowing = snapshot.value as? Int, isFollowing == 1 {
self.followButton.setTitle("Unfollow", for: .normal)
} else {
self.setupFollowStyle()
}
}, withCancel: { (err) in
print("Failed to check if following:", err)
})
}
}
因此,问题就出在这里。当我第一次访问所介绍的控制器时,它将根据这两种情况之一来渲染适当的单元格。但是,我第二次去那里时,它是从那里的旧标题中取出的,所以我的视图最终看起来像这样。它在哪里渲染当前登录用户的标头以及是否不是当前登录用户的标头。可以通过跟随按钮和编辑配置文件按钮的外观看到
如您所见,这显然是错误的,因为作为当前登录用户,我不应该关注自己。同样,如果您转到另一个用户个人资料,则可以编辑有关他们的信息,这样事情就一团糟
我添加的最后一件事是用户变量和相应的didSet方法
var user: User?{
didSet {
setupProfileImage()
// userNameLabel.text = user?.username
setupUserInteraction()
}
}
感谢您的帮助
var header: UserProfileHeader?
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
header = (collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerID, for: indexPath) as! UserProfileHeader)
header?.profileeSettings.addTarget(self, action: #selector(profileSettingsTapped), for: .touchUpInside)
header?.searchProfileViewController = self
header?.settings.addTarget(self, action: #selector(settingsButtonTapped), for: .touchUpInside)
header?.user = self.user
header?.backButton.addTarget(self, action: #selector(GoBack), for: .touchUpInside)
return header!
}
登录用户个人资料的图片
最佳答案
在第一个代码中,我看不到您在哪里保存userProfileController的变量。所以我猜userProfileController是类或全局var。这意味着每次您提供相同的控制器。
每次在
setupUserInteraction()
方法。但是没有removeFromSuperview方法。
关于ios - collectionView header 未正确更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48679223/