在下面的代码中,我使用发布日期和childAdded查询所有新帖子,但似乎当没有新项目时,表格视图不会显示除firebase块之外的任何项目还在跑?请帮忙。我确实添加了部分行数。我不知道为什么堆栈溢出不会显示。
FIRDatabase.database().reference().child("posts").child(uid!).queryOrdered(byChild: "postedDate").observe(.childAdded, with: {(snapshot) in
if (snapshot.hasChildren()) {
print("Has children")
if let dictionary = snapshot.value as? [String: AnyObject] {
let imageCount = dictionary["Images"]?.count
// Post not saved
if (imageCount == nil) {
print("image count is nil")
}
// better catch
else if (imageCount! < 3) {
print("not all 3 were posted")
}
// Saved post
else {
// Adding to details array
self.keyArray.append(snapshot.key)
// Inside a for loop to get every Image
for index in 1 ..< dictionary["Images"]!.count + 1 {
// Check = image1, image2 ...
let check = "image" + String(index)
// Put into the imageArray dictionary
self.imageArray[check] = dictionary["Images"]?[check] as! String?
}
// Handles adding the key and appending the image
self.imageArray2[snapshot.key] = self.imageArray
self.imageArray3.append(self.imageArray2)
// No images but actual values
// Creating makeEasy variables
var valuesArray = [String: String]()
// Adding values to specific array
let name = dictionary["Itemname"] as? String?
// I have all items
if (name != nil) {
// Add values to valueArray
valuesArray["name"] = name!
// Add the Key: UUID
self.details[snapshot.key] = valuesArray
// Then put in the actual array
self.detailsArr.append(self.details)
}
// I DONT have all items
else {
print("Not present!!")
}
// Removing
self.imageArray.removeAll()
self.imageArray2.removeAll()
valuesArray.removeAll()
self.details.removeAll()
}
}
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
else {
print("Has NO children")
}
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.imageArray3.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "viewTableCell", for: indexPath) as! viewTableViewCell
// get the key first and CHECK IF IT EXISTS ******
let key = keyArray[indexPath.row]
cell.name.text = self.detailsArr[indexPath.row][key]!["name"]
// cache the pic
if let pic = self.imageArray3[indexPath.row][key]?["image1"] {
cell.itemImage.load(pic)
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Set the key and the index
self.tappedIndex = indexPath.row
self.currentKey = keyArray[indexPath.row]
self.performSegue(withIdentifier: "mystify", sender: self)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400
}