TableView不打印任何单元格(swift)
我正在尝试将从firebase数据库提取的简单值显示到当前的TableView中。我遇到的问题是,这些单元格当前没有填充任何内容。我加入print语句只是为了确保数组不是空的,它不是空的。我正在使用以下代码:TableView不打印任何单元格(swift),swift,firebase,firebase-realtime-database,firebase-authentication,Swift,Firebase,Firebase Realtime Database,Firebase Authentication,我正在尝试将从firebase数据库提取的简单值显示到当前的TableView中。我遇到的问题是,这些单元格当前没有填充任何内容。我加入print语句只是为了确保数组不是空的,它不是空的。我正在使用以下代码: class DisplayTransactions: UITableViewController { var ref = FIRDatabase.database().reference() //gets current firebase users v
class DisplayTransactions: UITableViewController {
var ref = FIRDatabase.database().reference()
//gets current firebase users
var userC = FIRAuth.auth()?.currentUser
var pubCount: Int!
var anArray = [String] ()
override func viewDidLoad() {
super.viewDidLoad()
var transRef = FIRDatabase.database().reference().child("Users").child((userC?.uid)!).child("PaymentTransactions")
//for Deposit children
transRef.child("Deposit").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
//creates enumerator of the snapshot children for
let enumerator = snapshot.children
while let rest = enumerator.nextObject() as? FIRDataSnapshot {
self.anArray.append(String(rest.value))
//printing the rest.value to make sure there are children to iterate through
print(rest.value)
}
})
//for Withdrawl children
transRef.child("Withdraw").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
//creates enumerator of the snapshot children for
let enumerator = snapshot.children
while let rest = enumerator.nextObject() as? FIRDataSnapshot {
self.anArray.append(String(rest.value))
//printing the rest.value to make sure there are children to iterate through
print(rest.value)
}
})
print(anArray)
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return anArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "cellID")
cell.textLabel?.text = anArray[indexPath.row]
print(anArray[indexPath.row])
return cell
}
}
在填充数据源后,似乎没有重新加载UITableView。 每次更改数组的内容时,都需要这样做
override func viewDidLoad() {
super.viewDidLoad()
var transRef = FIRDatabase.database().reference().child("Users").child((userC?.uid)!).child("PaymentTransactions")
//for Deposit children
transRef.child("Deposit").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
//creates enumerator of the snapshot children for
let enumerator = snapshot.children
while let rest = enumerator.nextObject() as? FIRDataSnapshot {
self.anArray.append(String(rest.value))
//printing the rest.value to make sure there are children to iterate through
print(rest.value)
}
self.tableView.reloadData() //<<<<<<<<<<<< RELOAD TABLEVIEW
})
//for Withdrawl children
transRef.child("Withdraw").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
//creates enumerator of the snapshot children for
let enumerator = snapshot.children
while let rest = enumerator.nextObject() as? FIRDataSnapshot {
self.anArray.append(String(rest.value))
//printing the rest.value to make sure there are children to iterate through
print(rest.value)
}
self.tableView.reloadData() //<<<<<<<<<<<< RELOAD TABLEVIEW
})
print(anArray)
}
override func viewDidLoad(){
super.viewDidLoad()
var transRef=FIRDatabase.database().reference().child(“用户”).child((userC?.uid)!).child(“付款交易”)
//寄存儿童
transRef.child(“存款”).observeSingleEventOfType(.Value,带块:{(快照)in
//为创建快照子项的枚举数
让枚举数=snapshot.children
而让rest=enumerator.nextObject()作为?FIRDataSnapshot{
self.anArray.append(字符串(rest.value))
//打印rest.value以确保有子级要迭代
打印(剩余值)
}
self.tableView.reloadData()//我已经尝试过了,但是没有任何变化。仍然会显示一小段空白单元格,然后转到一个完全空白的页面。啊,我的错误,我把.reloadData()原来放错了位置。你需要将这些放在“块”内,而不是外部。