我正在尝试使用群组创建聊天应用程序。我需要这样做,以便用户可以删除组。我做了一切,除了一件事,它起作用了。如果用户删除组而不是创建具有其他名称的组。用户将看到已删除的组名称,而不是新的组名称。用户单击该组后,该名称将更新为新的组名。有谁知道如何解决这个问题?
组删除代码:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let cell = familyTableview.cellForRow(at: indexPath) as! FamilyTableViewCell
let familyName = cell.familyName.text!
guard let uid = FIRAuth.auth()?.currentUser?.uid else {
return
}
let ref = FIRDatabase.database().reference()
let usersReference = ref.child("Users").child(uid).child("Familys").child(familyName)
usersReference.removeValue { (removeError, ref) in
if(removeError != nil){
print(removeError as Any)
return
}
}
let familyNamesReference = ref.child("Family Names").child(familyName)
familyNamesReference.observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let familyUid = dictionary["UID"] as! String
let familysReference = ref.child("Familys").child(familyUid).child("Users").child(uid)
familysReference.removeValue(completionBlock: { (removeError, ref) in
if(removeError != nil){
print(removeError as Any)
return
}
})
}
}, withCancel: nil)
self.familys.remove(at: indexPath.row)
self.savedNames.removeAll()
self.savedFunctions.removeAll()
self.savedImages.removeAll()
self.namesOtherWay.removeAll()
DispatchQueue.main.async {
self.familyTableview.setEditing(false, animated: true)
self.familyTableview.deleteRows(at: [indexPath], with: .automatic)
self.familyTableview.reloadData()
}
}
创建单元代码:
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return familys.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = familyTableview.dequeueReusableCell(withIdentifier: "familyCell") as! FamilyTableViewCell
if(savedNames[indexPath.row] != nil && savedFunctions[indexPath.row] != nil && savedImages[indexPath.row] != nil){
cell.familyName.text = savedNames[indexPath.row]
cell.familyFunction.text = savedFunctions[indexPath.row]
cell.familyImage.image = savedImages[indexPath.row]
return cell
}
DispatchQueue.global(qos: .userInteractive).async {
let family = self.familys[indexPath.row]
cell.familyName.text = family.name
self.savedNames.updateValue(family.name!, forKey: indexPath.row)
self.namesOtherWay.updateValue(indexPath.row, forKey: family.name!)
cell.familyFunction.text = family.function
self.savedFunctions.updateValue(family.function!, forKey: indexPath.row)
let imageLink = family.imageLink
let url = URL(string: imageLink!)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if(error != nil){
print(error as Any)
return
}
DispatchQueue.main.async {
if let downloadedImage = UIImage(data: data!) {
let image = downloadedImage
cell.familyImage.image = image
self.savedImages.updateValue(image, forKey: indexPath.row)
self.familyTableview.reloadData()
}
}
}.resume()
}
return cell
}
我希望有人能够帮助我!感谢。
答案 0 :(得分:0)
也许我完全错了,但应该这部分
self.familys.remove(at: indexPath.row)
self.savedNames.removeAll()
self.savedFunctions.removeAll()
self.savedImages.removeAll()
self.namesOtherWay.removeAll()
DispatchQueue.main.async {
self.familyTableview.setEditing(false, animated: true)
self.familyTableview.deleteRows(at: [indexPath], with: .automatic)
self.familyTableview.reloadData()
}
不在这部分内部?
familysReference.removeValue(completionBlock: { (removeError, ref) in
if(removeError != nil){
print(removeError as Any)
return
}
// add the part here
})
因为可以在观察者识别出数据被删除之前重新加载表。
答案 1 :(得分:0)
经过几个小时的努力,我终于找到了解决方案!当我删除组(单元格)时,我只需要清除文本区域和uiimages。