从UITableView和Firebase删除单元格

时间:2018-10-17 03:47:53

标签: ios swift

我尝试删除单元格(向左滑动)并从Firebase数据库和存储中删除数据。使用此代码:

class  SizePanel extends JPanel{

    public SizePanel() {
        setLayout(new GridLayout(3,1));

        JLabel label1 = new JLabel();
        label1.setText("Size");

        // Create Radio buttons
        JRadioButton large = new JRadioButton("Large - $11.99 + $1.50 / reg topping + $2.00 / prem topping", true);
        JRadioButton xlarge = new JRadioButton("X-Large - $13.99 + $1.50 / reg topping + $2.00 / prem topping");
        JRadioButton small = new JRadioButton("Small - $7.99 + $1.00 / reg topping + $1.50 / prem topping");

        // Group the radio buttons
        ButtonGroup sz = new ButtonGroup();
        sz.add(large);
        sz.add(xlarge);
        sz.add(small);
        add(label1);
        add(large);
        add(xlarge);
        add(small);
    }
}

我有一个变种

struct Records {

let title: String
let source: String
var length: String
let recordUrl: String
let username: String

init(dictionary: [String: Any]) {
    self.title = dictionary["title"] as? String ?? ""
    self.source = dictionary["source"] as? String ?? ""
    self.length = dictionary["length"] as? String ?? ""
    self.recordUrl = dictionary["recordUrl"] as? String ?? ""
    self.username = dictionary["username"] as? String ?? ""
}
}

试图删除单元格

var records = [Records]()

Firebase JSON enter image description here

我需要删除“记录”中的项目(在TableView中显示为单元格),但是现在使用我的代码删除了整个“记录”

2 个答案:

答案 0 :(得分:0)

您还需要保存记录的ID。这样,您可以创建该特定记录的路径。之后,只需在路径中再添加一个孩子即可。

喜欢这个。

Database.database().reference().child("users").child(uid).child("records").child(record.id).removeValue()

答案 1 :(得分:0)

有一些新东西。可以获取分支“记录”中所有项目的键

guard let uid = Auth.auth().currentUser?.uid else { return }
let ref = Database.database().reference().child("users").child(uid).child("records")
ref.observeSingleEvent(of: .value, with: { (snapshot) in

guard let dictionaries = snapshot.value as? [String: Any] else { return }
let record = self.records[indexPath.item]
dictionaries.forEach({ (key, value) in
                print("Key \(key)")

但仍然无法获得要删除当前项目的ID(记录在“记录”分支中)