因此,我正在从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个孩子显示其孩子等于“是”,并且没有错误,因为我根本没有做任何更改。
答案 0 :(得分:1)
您可以执行以下一项操作:
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()
})