我的代码没有将while
的{{1}}语句中的值添加到UIViewController
的数组中。
这是一个getter函数,允许我查看其他子项下的所有子项值。现在,我将更具体:
我的数据库节点由以下组成:
汽车-> 0、1、2、3,...->型号,价格,...->字符串
如您所见,孩子的数量是不确定的,因此我必须使用此控制方法:
UITableViewController
首先,在加载while let child = snapshotChildren.nextObject() as? DataSnapshot {
// Get code node key and save it to cars array
}
中,我获得了car的代码节点密钥,并将它们保存到ViewController
的{{1}}类型的car变量中。然后,我将在NSMutableArray
中执行相同的操作,以获取所有TableViewController
个孩子的值。
TableViewController
结果是,使用此代码,我仍然有空的indexpath.row
。我该怎么解决?
编辑1
我修复了该片段的问题:
let rootRef = Database.database().reference()
let carconditionalRef = rootRef.child("Cars")
carconditionalRef.observe(.value) {(snap: DataSnapshot) in
//Get all the children from snapshot you got back from Firebase
let snapshotChildren = snap.children
//Loop over all children (code) in Firebase
while let child = snapshotChildren.nextObject() as? DataSnapshot {
// Get code node key and save it to cars array
let carvc = Cars_Table();
carvc.cars.add(child.key)
}
}
编辑2
我尝试使用递归方法,但是没有用。因此,这次我使用NSMutableArray
语句尝试了一次迭代方法。
这是我的新功能,这次直接在import UIKit
import FirebaseDatabase
class Loading: UIViewController {
@IBOutlet weak var loading: UIActivityIndicatorView!
var mother: NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
start()
}
func start() {
loading.startAnimating()
if #available(iOS 10.0, *) {
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { (timer) in
//let's dance
self.loading.startAnimating()
//call data from database
let rootRef = Database.database().reference()
let conditionalRef = rootRef.child("Cars")
conditionalRef.observe(.value) {(snap: DataSnapshot) in
// Get all the children from snapshot you got back from Firebase
let snapshotChildren = snap.children
// Loop over all children (code) in Firebase
while let child = snapshotChildren.nextObject() as? DataSnapshot {
// Get code node key and save it to cars array
self.mother.add(child.key)
}
self.move()
self.loading.stopAnimating()
self.performSegue(withIdentifier: "loadingfinish", sender: nil)
}
}
} else {
// Fallback on earlier versions
}
}
func move() {
let vc = Cars_Table()
vc.cars = self.mother
}
}
中:
while
当我去做Car_TableView.swift
时,func loadData() {
//call data from database
let rootRef = Database.database().reference()
let conditionalRef = rootRef.child("Cars")
conditionalRef.observe(.value) {(snap: DataSnapshot) in
// Get all the children from snapshot you got back from Firebase
let snapshotChildren = snap.children
// Loop over all children (code) in Firebase
while let child = snapshotChildren.nextObject() as? DataSnapshot {
// Get code node key and save it to cars array
self.populateTable.append(child.key)
}
var counter = 0
while counter > -self.populateTable.count {
counter -= 1
let rootRef = Database.database().reference()
let userRef = rootRef.child("Cars").child("\(self.populateTable.count+counter)")
userRef.observeSingleEvent(of: .value, with: { snapshot in
let userDict = snapshot.value as! [String: Any]
let model1 = userDict["Model"] as! String
self.model.add(model1)
let detail1 = userDict["Detail"] as! String
self.detailpage.add(detail1)
let year1 = userDict["Year"] as! String
self.year.add(year1)
let carPrice1 = userDict["Price"] as! String
self.price.add(carPrice1)
let carimageURL1 = userDict["imageURL"] as! String
self.imagePathString.add(carimageURL1)
}) //end observeSingleEvent
}
}
}
可以工作,但是会重复n ^ 2次。为什么会这样?
编辑3
由于该问题似乎从一开始就已经改变,所以我进行了编辑以提供所有相关的详细信息。因此,现在的问题有所不同,现在有两个:
while
,然后触摸标签栏按钮以返回到observeSingleEvent
对于第一个问题...至少我不知道为什么会发生
对于第二个问题,我想使用ViewController
重新加载数据,但不适用于Car_TableView.swift
函数,如果我尝试实例方法SVProgressHUD
,它将崩溃。
变量都是loadData()
,因为我必须加载很多可以随时间变化的东西
您可以看到,我的tableView.reloadData()
函数非常简单:
NSMutableArray
这是我们viewDidLoad()
中的Table视图数据源:
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
我还重新发布了Car_TableView.swift
函数,因为我在仅while语句中简化了操作:
override func numberOfSections(in tableView: UITableView) -> Int {
return populateTable.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return populateTable.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "carTableCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! Car_Cell
cell.carLabel?.text = "\(self.model[indexPath.row])"
cell.carSubtitle?.text = "Year: \(self.year[indexPath.row]) - Price: \(self.price[indexPath.row])$"
Alamofire.request("\(self.imagePathString[indexPath.row])").response { response in
guard let image = UIImage(data:response.data!) else {
// Handle error
return
}
let imageData = image.jpegData(compressionQuality: 1.0)
cell.carImage.contentMode = .scaleAspectFit
cell.carImage.image = UIImage(data : imageData!)
}
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowcarDetails" {
let myIndexPath = self.tableView.indexPathForSelectedRow!
//save detail page url in UserDefault
let SVDetail = self.detailpage[myIndexPath.row]
let SVDetaildefaults = UserDefaults.standard
SVDetaildefaults.set(SVDetail, forKey: "sv_detail")
SVDetaildefaults.synchronize()
_ = segue.destination
as! Car_Detail
}
}
//SET CELLS SIZE
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.row {
case 0,1,2,3,4:
return 100
default:
return 100
}
}