首页 > 解决方案 > 如何根据json值firebase实时数据库使单元格自行删除

问题描述

因此,我正在从 firebase 实时数据库中检索数据,并且我正在将其全部提取出来,并且我需要每个单元格显示父俱乐部,只有当它的子“Promoted”的值为“Yes”并且如果它不需要时,我需要该单元格删除自己,只有 3 个俱乐部曾经升级等于“是”

它全部拉出,并全部显示在屏幕上,但我被困在如何删除孩子们没有提升等于“是”的单元格上

//tempViewController.swift
import Foundation
import UIKit
import FirebaseDatabase

class tempViewController: UIViewController ,  UITableViewDelegate , UITableViewDataSource {

    @IBOutlet weak var tempTableView: UITableView!
    var finalBar = [NightClubs]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tempTableView.dataSource = self
        tempTableView.delegate = self

        DataService.ds.REF_BARS.observeSingleEvent(of: .value, with: { (snapshot) in
            print(snapshot.value as Any)
            if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
                for snap in snapshot {
                    print(snap)
                    if let barData = snap.value as? Dictionary<String, AnyObject> {
                        let bar = NightClubs(barData: barData)
                        self.finalBar.append(bar)
                        print(self.finalBar)
                        self.tempTableView.reloadData()
                    }
                    self.tempTableView.reloadData()
                }
                self.tempTableView.reloadData()
            }
            self.tempTableView.reloadData()
        })
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView( _ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  finalBar.count
    }

    func tableView( _ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tempTableView.dequeueReusableCell(withIdentifier: "newCell") as! newCell
        let barz = finalBar[indexPath.row]

        if barz.promoted == "Yes" {
            cell.setData(data: barz)
            // this sends the nightclubs that have .promoted == "Yes"
        } else {
            // need to remove the cell from the user view
            // as only 3 NightClubs will have promoted == "Yes"
            // so i only want to see 3 cells
        }
        return cell
    }
}
//newCell.swift
import Foundation
import UIKit

class newCell: UITableViewCell {


    @IBOutlet weak var nameTextLabel: UILabel!
    @IBOutlet weak var locationTextLabel: UILabel!



    func setData(data: NightClubs) {
        nameTextLabel.text = data.name
        locationTextLabel.text = data.location
    }


}
//NightClubs.swift
import Foundation
import UIKit

class NightClubs {

    private var _name: String!
    private var _location: String!
    private var _address: String!
    private var _latitude: String!
    private var _longitude: String!
    private var _promoted: String!
    private var _type: String!
    private var _liveCount: String!
    private var _goingCount: String!
    private var _description: String!

    var name: String! {
        return _name
    }

    var location: String! {
        return _location
    }

    var address: String! {
        return _address
    }

    var latitude: String! {
        return _latitude
    }

    var longitude: String! {
        return _longitude
    }

    var promoted: String! {
        return _promoted
    }

    var type: String! {
        return _type
    }

    var liveCount: String! {
        return _liveCount
    }

    var goingCount: String! {
        return _goingCount
    }

    var description: String! {
        return _description
    }

    init(name: String, location: String, address: String, latitude: String, longitude: String, promoted: String, type: String, liveCount: String, goingCount: String, description: String) {
        self._name = name
        self._location = location
        self._address = address
        self._latitude = latitude
        self._longitude = longitude
        self._promoted = promoted
        self._type = type
        self._liveCount = liveCount
        self._goingCount = goingCount
        self._description = description
    }

    init(barData: Dictionary<String, AnyObject>) {
        if let name = barData["Name"] as? String {
            self._name = name
        }
        if let location = barData["Location"] as? String {
            self._location = location
        }
        if let address = barData["Address"] as? String {
            self._address = address
        }
        if let latitude = barData["Latitude"] as? String {
            self._latitude = latitude
        }
        if let longitude = barData["Longitude"] as? String {
            self._longitude = longitude
        }
        if let promoted = barData["Promoted"] as? String {
            self._promoted = promoted
        }
        if let type = barData["Type"] as? String {
            self._type = type
        }
        if let liveCount = barData["LiveCount"] as? String {
            self._liveCount = liveCount
        }
        if let goingCount = barData["GoingCount"] as? String {
            self._goingCount = goingCount
        }
        if let description = barData["Description"] as? String {
            self._description = description
        }
    }
}

我希望它基本上只显示 3 个孩子,其中它的孩子等于“是”并且没有错误,因为我根本没有做任何事情来改变它

标签: jsonswiftxcodefirebasefirebase-realtime-database

解决方案


您可以执行以下操作之一:

1) 在 finalBar 数组中添加唯一的柱,其提升值为“是”。对于此更新,您的数据获取代码应该是这样的。

DataService.ds.REF_BARS.observeSingleEvent(of: .value, with: { (snapshot) in
    print(snapshot.value as Any)
    if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
        for snap in snapshot {
            print(snap)
            if let barData = snap.value as? Dictionary<String, AnyObject> 
            {
               let bar = NightClubs(barData: barData)
               // Add this check here
               if bar.promoted == "Yes" {
                   self.finalBar.append(bar)
               }
               print(self.finalBar)
            }
        }
    }
    self.tempTableView.reloadData()
 })

2) 或创建另一个数组并过滤 finalBar 数组,使其仅给出提升为“是”的条并将其存储在不同的数组中。然后使用这个新数组来显示你的单元格。

例如filteredFinalBars = finalBar.filter { $0.promoted == "Yes"}

 DataService.ds.REF_BARS.observeSingleEvent(of: .value, with: { (snapshot) in
        print(snapshot.value as Any)
    if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
            for snap in snapshot {
                print(snap)
                if let barData = snap.value as? Dictionary<String, AnyObject> 
                {
                   let bar = NightClubs(barData: barData)
                   self.finalBar.append(bar)
                   print(self.finalBar)
                }
            }
            //Add this to filter the bars with promoted as "yes".
            //Then use this array in tableView delegate and datasource methods
            filteredFinalBars = finalBar.filter { $0.promoted == "Yes"}
        }
        self.tempTableView.reloadData()
     })

推荐阅读