0
我讓我的用戶按下單元格中的按鈕,這會從Firebase後端移除數據。我希望細胞在同一時間被移除。它將從firebase數據庫中刪除,但不會從collectionView中刪除。如果我離開視圖並返回到collectionView,它將被刪除。
如何在按下按鈕後將其移除,並將其留在視圖中?UICollectionView數據在刪除Firebase後未被移除
func didSelect(for cell: StaffSearchCell) {
guard let indexpath = collectionView?.indexPath(for: cell) else { return }
let studentUser = self.filteredUsers[indexpath.item]
let selectedUserId = studentUser.uid
guard let staffUID = Auth.auth().currentUser?.uid else { return }
print("1st")
// Add student to staff list
let ref = Database.database().reference().child("staffUsers").child(staffUID).child("studentSession1List")
let values = [selectedUserId: true]
ref.updateChildValues(values) { (err, ref) in
if let err = err {
print("Failed to follow user:", err)
return
}
print("student to staff")
}
// Add selected staff to student list
let studentRef = Database.database().reference().child("studentUsers").child(selectedUserId).child("studentSession1List")
studentRef.removeValue()
let staffValues = [staffUID: true]
studentRef.updateChildValues(staffValues) { (err, studentRef) in
if let err = err {
print("Failed to follow user:", err)
return
}
print("staff to student")
}
let allStudentRef = Database.database().reference()
allStudentRef.child("session1AllStudents").child(selectedUserId).removeValue() { (error, ref) in
if error != nil {
print("error \(error)")
}
print("removed from session1AllStudents")
self.fetchRemainingStudents()
}
searchBar.isHidden = true
searchBar.resignFirstResponder()
// self.collectionView?.reloadData()
//self.navigationController?.popViewController(animated: true)
}
var filteredUsers = [User]()
var users = [User]()
func fetchRemainingStudents() {
print("second")
let ref = Database.database().reference()
ref.child("session1AllStudents").observeSingleEvent(of: .value, with: { (snapshot) in
HUD.show(.labeledProgress(title: "Finding Students", subtitle: nil))
for snap in snapshot.children {
let studentsSnap = snap as! DataSnapshot
let studentsKey = studentsSnap.key
let studentDict = snapshot.value as! [String: Any]
var aStudent = User(uid: studentsKey, dictionary: studentDict)
let userRef = ref.child("studentUsers").child(studentsKey)
userRef.observeSingleEvent(of: .value, with: { snapshot in
let userDict = snapshot.value as! [String:AnyObject]
let firstName = userDict["firstName"] as! String
let lastName = userDict["lastName"] as! String
let email = userDict["email"] as! String
aStudent.firstName = firstName
aStudent.lastName = lastName
aStudent.email = email
self.users.append(aStudent)
self.filteredUsers = self.users
// self.users.sort(by: { (u1, u2) -> Bool in
//
// return u1.lastName.compare(u2.lastName) == .orderedAscending
// })
HUD.hide()
self.collectionView?.reloadData()
})
}
})
}
細胞:
protocol StaffSearchCellDelegate {
func didSelect(for cell: StaffSearchCell)}
類StaffSearchCell:UICollectionViewCell {
var delegate: StaffSearchCellDelegate?
var user: User? {
didSet {
lastNameLabel.text = user?.lastName
firstNameLabel.text = user?.firstName
guard let email = user?.email else { return }
if email == "" {
emailLabel.text = ""
} else {
emailLabel.text = user?.email
}
}
}
let firstNameLabel: UILabel = {
let label = UILabel()
label.text = "First Name"
label.font = UIFont.boldSystemFont(ofSize: 12)
return label
}()
let lastNameLabel: UILabel = {
let label = UILabel()
label.text = "Last Name"
label.font = UIFont.boldSystemFont(ofSize: 12)
return label
}()
let emailLabel: UILabel = {
let label = UILabel()
label.text = "@email"
label.font = UIFont.systemFont(ofSize: 11)
label.textColor = UIColor.lightGray
label.textAlignment = .left
return label
}()
lazy var addSessionButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Add", for: .normal)
button.addTarget(self, action: #selector(handleSelectSession), for: .touchUpInside)
button.backgroundColor = UIColor.bulldogGold().withAlphaComponent(0.75)
button.setTitleColor(UIColor.black, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
button.layer.cornerRadius = 5
return button
}()
func handleSelectSession() {
print("student selected")
delegate?.didSelect(for: self)
}
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(firstNameLabel)
addSubview(lastNameLabel)
addSubview(emailLabel)
addSubview(addSessionButton)
firstNameLabel.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: lastNameLabel.leftAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 4, width: 0, height: 0)
lastNameLabel.anchor(top: topAnchor, left: firstNameLabel.rightAnchor, bottom: bottomAnchor, right: emailLabel.leftAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 4, width: 0, height: 0)
emailLabel.anchor(top: topAnchor, left: lastNameLabel.rightAnchor, bottom: bottomAnchor, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 8, width: 0, height: 0)
addSessionButton.anchor(top: topAnchor, left: nil, bottom: bottomAnchor, right: rightAnchor, paddingTop: 4, paddingLeft: 4, paddingBottom: 4
, paddingRight: 4, width: 50, height: 0)
let separatorView = UIView()
separatorView.backgroundColor = UIColor(white: 0, alpha: 0.5)
addSubview(separatorView)
separatorView.anchor(top: nil, left: firstNameLabel.leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0.5)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}}