我有一个搜索控制器,当点击时会显示一个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!
}
答案 0 :(得分:0)
在第一段代码中,我不知道你持有userProfileController变量的位置。所以我猜测userProfileController是类或全局var。这意味着每次出现相同的控制器。
每次在
中添加子视图setupUserInteraction()
方法。但是没有removeFromSuperview方法。