我目前正在使用Firebase来存储人员的数据以及有关他们的一些细节。这是我从数据库中检索细节以填充我的tableView:
的方法 let ref = FIRDatabase.database().reference().child("Users")
ref.observeEventType(.ChildAdded, withBlock:
{ (snapshot) in
if let firstname = snapshot.value?.objectForKey("firstname"), lastname = snapshot.value?.objectForKey("lastname")
{
let fullname = "\(firstname) \(lastname)"
self.names.append(fullname)
self.tableView.reloadData()
}
}, withCancelBlock: nil)
我的每个用户都有自己唯一的ID,我试图根据它删除它们。到目前为止,我只从tableView端删除我的用户,而不是从数据库中删除:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == .Delete
{
names.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
//Delete from firebase
}
}
在我的observePeople()函数中,我实现了代码,以观察是否像这样删除了一个子代:
ref.observeEventType(.ChildRemoved, withBlock:
{ (snapshot) in
if let firstname = snapshot.value?.objectForKey("firstname"), lastname = snapshot.value?.objectForKey("lastname"), dateOfBirth = snapshot.value?.objectForKey("Date of Birth"), zipcode = snapshot.value?.objectForKey("Zipcode")
{
print("We deleted the person \(firstname) \(lastname) with the details: \(dateOfBirth), \(zipcode)")
}
}, withCancelBlock: nil)
那么当有人通过从tableView中滑动删除确切的用户时,如何根据他们的ID删除确切的用户?
答案 0 :(得分:1)
您可以在要删除用户的引用处使用removeValue()
方法。
因此,在您的commitEditingStyle
表格视图方法中,您在//Delete from firebase
处写了以下内容:
ref.child("Users/\(uniqueUserID)").removeValue()
您需要在表格视图中删除的uniqueUserID
处获取该用户的indexPath.row
。
如果您输入一个JSON树为您的用户提供的示例,我可以详细介绍。
是这样的吗?
"root"
"Users"
"uniqueUserID"
"firstname"
"lastname"
"dateOfBirth"
"zipcode"
修改强> 如果你的字典看起来像上面那样我会做这样的事情:
在ref.observeEventType(.ChildAdded
:
//names will be the array that stores the user data that you retrieve from Firebase
//names will be an array of dictionaries
//each dictionary will represent a user object that includes firstname, lastname, AND uniqueUserID
self.names = [[String : AnyObject]]()//make a new clean array
//create a dictionary to store the user data
let user = snapshot.value as! [String: AnyObject]
//get the uniqueUserID which is the `snapshot.key`
let uniqueUserID = snapshot.key as! String
//add the uniqueUserID to the user dictionary
user["uniqueUserID"] = uniqueUserID as! String
self.name.append(user)
self.tableView.reloadData()
在您编写commitEditingStyle
的{{1}}表格视图方法中:
//Delete from firebase
答案 1 :(得分:0)
通过匹配全名,我能够找到快照的密钥,即用户的唯一ID。但是我确实遇到了一个错误,在我删除一个项目之后它会删除它后面的索引,直到我得到剩下的一个项目并且我得到“索引超出范围错误”。
let ref = FIRDatabase.database().reference().child("Users")
ref.observeEventType(.ChildAdded, withBlock: { (snapshot) in
if let firstname = snapshot.value?.objectForKey("firstname"), lastname = snapshot.value?.objectForKey("lastname")
{
let fullname = "\(firstname) \(lastname)"
let currentName = self.names[indexPath.row]
if fullname == currentName
{
print("We have a match")
let currentKey = snapshot.key
ref.child(currentKey).removeValue()
dispatch_async(dispatch_get_main_queue())
{
self.tableView.reloadData()
}
}
}
}, withCancelBlock: nil)