【问题标题】:Firebase: delete item from row (Swift 3)Firebase:从行中删除项目(Swift 3)
【发布时间】:2016-12-09 18:41:23
【问题描述】:

我有一个表格视图,显示来自 Firebase 控制台(JASON 结构)的数据,如下所示:

chklst-xxx
     - avaliation
         -students
              -KHZAjBi44eZ8JsaswkKI
                -teacher: Paul 
                -datTime: 09.12.2016  12:25pm
                -name: Mary Anne
                -mark: 7.5 
                -preceptor: John 
              -KHWZlh7aasy78ahsiuKIi0
                -teacher: Paul 
                -datTime: 09.12.2016  12:48pm
                -name: Anne Caroline
                -mark: 9.4 
                -preceptor: Peter

这些数据显示在一个表视图中,每一个都在节点学生之后,除了键。我有一个删除特定行的功能,就像在 CoreData 中的其他应用程序一样(tableview editingStyle):

// FUNC DELETAR
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        teste.remove(at: indexPath.row)
        ref.removeValue() // HERE IS MY DOUBT
        tableView.reloadData()
    }
}

我可以从数组和表格中删除项目,但我只是不知道如何从 Firebase 中删除。我已经尝试过使用 removeValue 的其他方法,但它只是没有用,除了一种擦除整个数据的方法,而不是特别是选择的那一行。

let ref = FIRDatabase.database().reference().child("avaliation").child("students")

我需要一些教学来完成这项工作。谢谢。

这是我保存此数据的方法:(添加于 09.12 晚上 19​​:30)

func Save(){
    let ref = FIRDatabase.database().reference().child("avaliation").child("students").childByAutoId()
    referencia.child(“name”).setValue(Name)
    referencia.child(“teacher”).setValue(Teacher)
    referencia.child("preceptor").setValue(Preceptor)
    referencia.child("datTime”).setValue(DateTime)
    referencia.child(“mark”).setValue(ResultComFReFC)
}

这是我使用主数组的类:

import Foundation
import Firebase

struct ChkList {
var name: String?
var teacher: String?
var preceptor: String?
var DateToday: String?
var mark: String?
var key: String!

init(name: String, teacher: String, preceptor: String, mark: String, DateToday: String, key: String = "") {
    self.name = name
    self.teacher = teacher
    self.preceptor = preceptor
    self.mark = mark
    self.DateToday = DateToday
    self.key = key
}


init(snapshot: FIRDataSnapshot) {
    key = snapshot.key
    let snapshotValue = snapshot.value as! [String: AnyObject]
    name = snapshotValue["name"] as? String
    teacher = snapshotValue["teacher"] as? String
    preceptor = snapshotValue["preceptor"] as? String
    mark = snapshotValue["mark"] as? String
    DateToday = snapshotValue["DateToday"] as? String
}

func toAnyObject() -> Any {
    return [
        "name": name,
        "teacher": teacher,
        "preceptor": preceptor,
        "mark": mark,
        "DateToday": DateToday   ]}}

更新:Tableview EdityingStyle

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

  if editingStyle == .delete {
let ref = FIRDatabase.database().reference().child("avaliation").child("students").childByAutoId()
teste.remove(at: indexPath.row) //Array delet item
      let userKeyA = ref.key
      ref.child(userKeyA).removeValue()
      tableView.reloadData()
        }
    }

【问题讨论】:

    标签: uitableview firebase swift3


    【解决方案1】:

    我的建议,当你push用户详细信息时,使用'getKey'方法存储用户的pushKey

    chklst-xxx
     - avaliation
         -students
              -KHZAjBi44eZ8JsaswkKI
                -teacher: Paul 
                -userKey:KHZAjBi44eZ8JsaswkKI <--------------
                -datTime: 09.12.2016  12:25pm
                -name: Mary Anne
                -mark: 7.5 
                -preceptor: John
    

    现在您可以检索用户的“密钥”。假设您在列表视图中列出用户并且想要删除一行。

    let ref = FIRDatabase.database().reference().child("avaliation").child("students").child(students.getUserKey()).removeValue();
    

    如果可行,请告诉我。

    更新:我不知道 swift。但我会给你一个抽象的想法。
    当你创建一个新学生时,你引用student nodepush() 对。如下所示,

    DatabaseReference studentsRef = FIRDatabase.database().reference().child("avaliation").child("students").push();
    

    然后你把你的数据像,

    studentRef.child("teacher").setValue("Paul");
    studentRef.child("userKey").setValue(studentRef.getKey()); <------------
    

    【讨论】:

    • 好的@Nishanth。谢谢你。我在这个问题上又添加了 2 个项目:我保存数据的方法和我的数组 ChkList 所在的类。我只是不知道如何像您一样从该用户密钥中插入和保存数据(func Save())以及如何调用这些 getuserkey 来删除该行。感谢您提供更多帮助
    • 嗨@Nishanth。第一步没问题:检索用户的“密钥”作为 userKey。但是这个用于删除的命令行不起作用。我认为这是一个 Swift 问题,因为它甚至在最后一部分输入 child(students. ) 之前就显示了错误消息:....child(students.getUserKey.removeValue())。我现在将与 Swift 程序员一起寻找更多参考资料,但无论如何都非常感谢。我从你的建议中学到了很多东西。
    • 真的很抱歉。有错别字!编辑了我的解决方案..现在检查! child("students").child(students.getUserKey()).removeValue();
    • 我很高兴我的建议很有帮助。欢迎你:)
    • 我又试了一次,但我不知道如何获取,使用students.getUserKey(),是如何获取该特定行的userKey,当我选择它时选择它。
    【解决方案2】:

    我做到了,使用上面的建议并学习和尝试了很多。 该方法的工作方式实际上是捕获用户密钥。

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    
    if editingStyle == .delete {
    
            let user: ChkList = teste[indexPath.row]
            let uk = user.key
            print("Userkey: \(uk)")
            ref.child(uk!).removeValue()
    }
    }
    

    这样,您将从 Firebase 和行表视图中删除数据。

    【讨论】:

      【解决方案3】:

      这个答案在幕后移动 Firebase 删除行为,以便 不干扰正常的 tableview 加载机制。一 好处是减少对 Firebase 的调用次数并删除 来自内部 tableview 动画过程的时间问题。

      对我有用的是让 Firebase 的东西不碍事。就我而言,我收集了所有我想要的密钥。在删除过程中,我从本地对象中删除了值,当离开场景时,我从 viewDidDisappear() 调用我的清理方法以将更新的数据同步回 Firebase:

      func syncFavorites() {
          //ref is the leaf for the favorites
          //clear existing
          ref?.removeValue()
          //temp collection
          var faves = [String:Any]()
          //dataArrayKeys is my collection of keys on self
          dataArrayKeys.keys.forEach { (key) in
            faves.updateValue("true", forKey: key)
          }
          ref?.updateChildValues(faves)
        }
      

      我使用常规方法处理我的tableview's 删除操作。

      override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
          if editingStyle == .delete {
            // Delete the row from the data source
            if let key = dataArr[indexPath.row]["key"] as? String {
              dataArrayKeys.removeValue(forKey: keyToDelete)
              dataArr.remove(at: indexPath.row)
            }
            tableView.deleteRows(at: [indexPath], with: .fade)
            self.tableView.reloadData()
          }
        }
      

      我毫不怀疑可能有更好的方法。这是对我有用的最简单的事情。

      【讨论】:

        猜你喜欢
        • 2017-12-02
        • 2018-10-30
        • 1970-01-01
        • 2017-12-06
        • 2017-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-15
        相关资源
        最近更新 更多