swift - 使用 FirebaseUI 删除 Firebase 数据库中的子项

标签 swift uitableview firebase firebase-realtime-database firebaseui

FirebaseUI 跟踪数据库中的更改并将其与我的 TableView 同步,这很好。我需要用户在滑动 tableView 单元格时能够自行删除子项。

我正在使用 populateCellWithBlock 来填充我的表格。但是,我找不到有关如何删除 child 的任何信息。 我有什么:

override func viewDidLoad() {
    super.viewDidLoad()

    dataSource = FirebaseTableViewDataSource.init(query: getQuery(),prototypeReuseIdentifier: "Cellident", view: self.tableView)

    dataSource?.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
        //Filling the table:

        let customCell = cell as! CustomCellTableViewCell

        let snap = obj as! FIRDataSnapshot
        let childString = snap.value as! [String : AnyObject]
        let data = childString["data"] as? String

        customCell.urlLabel.text = String(data!)

    }
    tableView.dataSource = dataSource
    tableView.delegate = self
}

现在,我已经尝试了几种使用数组的方法,但不知道如何使它们保持同步。我的问题是我不知道如何删除特定路径上的子项,即位于单个单元格中的子项。如何确保滑动删除的单元格触发从该单元格中删除子项?

我的问题是,如果我希望用户能够删除 child ,那么这里的最佳实践是什么。显然它需要从数据库和 tableView 中删除,但由于 FirebaseUI 会跟踪更改,我确信一定有一种方法使用 FirebaseUI 来删除它?

最佳答案

经过一番尝试,我找到了解决方案。我使用 MGSwipeTableCell 来使单元格可滑动并显示删除按钮,然后在它的方便回调中,我简单地做了:

let removebtn = MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor(), callback: {
            (sender: MGSwipeTableCell!) -> Bool in

            snap.ref.removeValue() //This

            return true
        })

快照在哪里:

let snap = obj as! FIRDataSnapshot

关于swift - 使用 FirebaseUI 删除 Firebase 数据库中的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39108181/

相关文章:

ios - Xcode 无法读取 swift 文件

ios - 自定义推送通知未使用 firebase 接收

ios - 修改 tableFooterView 到 tableView 的最后一个单元格和 tableView 的边距的距离

javascript - 如何在 Firebase 的 Cloud Firestore 中添加这样的数据?

firebase - Flutter Provider显示来自错误用户的数据

firebase - Flutter Web Firebase Hosting XMLHttpRequest 错误(使用本地 chrome 浏览器(调试)或使用 Firefox 均无错误)

swift - AVAudioPCMBuffer 以编程方式构建,不以立体声播放

ios - UITextfield 委托(delegate)响应所有文本字段

ios - 在静态 UITableView 上插入行

objective-c - iOS - iPhone 5 无法滚动到 TableView 的底部