【发布时间】:2017-12-20 00:18:46
【问题描述】:
我的数据库中有一个名为“汽车”的文件夹,该文件夹中有一个汽车品牌列表,我想检索所有品牌并将其放在 UITableView 中。然后,当您按下品牌时,它将显示该品牌的型号。我目前无法检索汽车列表。这是我的数据库和视图控制器代码的屏幕截图。
import UIKit
import Firebase
import FirebaseDatabase
import SDWebImage
struct carStruct {
let cars : String!
}
class CarMakeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var cars = [carStruct]()
override func viewDidLoad() {
super.viewDidLoad()
let ref = Database.database().reference().child("Cars")
ref.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.childrenCount)
for rest in snapshot.children.allObjects as! [DataSnapshot] {
guard let value = rest.value as? Dictionary<String,Any> else { continue }
guard let make = value["Cars"] as? String else { continue }
let cars = carStruct(cars: make)
self.cars.append(cars)
}
self.cars = self.cars.reversed(); self.tableView.reloadData()
})
}
@IBOutlet weak var tableView: UITableView!
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cars.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellMake")
let label1 = cell?.viewWithTag(21) as! UILabel
label1.text = cars[indexPath.row].cars
return cell!
}
}
【问题讨论】:
-
当您说您目前无法获取汽车列表时,当您尝试获取它们时会发生什么?
-
没有出现@JenPerson
-
在后台线程上更新 UI 是否存在问题?试试
DispatchQueue.main.async { self.tableView.reloadData( ) }之类的东西。 *免责声明:还没有完全研究过代码,只是第一个跳出来的。 -
我不认为那是问题,我仔细检查了。我使用相同的代码来检索用户,它工作正常,但不知道为什么这不起作用。
-
不起作用@TaylorM
标签: ios swift firebase swift3 firebase-realtime-database