我有一个看起来像这个图像的UITableView
.
当我滑动删除记录时,我可以将其从存储它的数组中完全删除,但是我在Firebase中访问它时很难将其删除.
对于上面的屏幕截图,我的Firebase数据库结构如下:
-KWc7RTuOe5PefiMM2tL bodyPart: "Arms" exerciseName: "Test 1 " userId: "8rHmyTxdocTEvk1ERiiavjMUYyD3" -KWcEbpw_f6kxcePY5cO bodyPart: "Chest" exerciseName: "Test 2 " userId: "8rHmyTxdocTEvk1ERiiavjMUYyD3" -KWcEdUN49QaJIVf0kwO bodyPart: "Legs" exerciseName: "Test 3 " userId: "8rHmyTxdocTEvk1ERiiavjMUYyD3" -KWcFrMSaLKQRxghGHyT bodyPart: "Arms" exerciseName: "Test 4" userId: "8rHmyTxdocTEvk1ERiiavjMUYyD3"
如何访问创建时设置的autoId值,例如“-KWc7RTuOe5PefiMM2tL”,以便删除该子节点?
或者我可以根据登录的UserId访问exerciseName值吗?
解决方法
继MHDev已回答之后:
func tableView(_ tableView: UITableView,commit editingStyle: UITableViewCellEditingStyle,forRowAt indexPath: IndexPath) { if editingStyle == .delete { if let exerciseName = exercises[indexPath.row].exerciseName { let ref = FIRDatabase.database().reference().child("userExercises") ref.queryOrdered(byChild: "exerciseName").queryEqual(toValue: exerciseName).observe(.childAdded,with: { (snapshot) in snapshot.ref.removeValue(completionBlock: { (error,reference) in if error != nil { print("There has been an error:\(error)") } }) }) } exercises.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath],with: .left) } }