【发布时间】:2020-05-17 15:59:21
【问题描述】:
当我转到另一个屏幕并返回我的数据显示时,我的 TableView 单元格没有加载? 任何解决此问题的方法
我的代码:
import UIKit
import FirebaseDatabase
var mynamesArray = [String]()
class Services: UIViewController, UITableViewDelegate, UITableViewDataSource {
var refHandle: UInt!
@IBOutlet var tableView: UITableView!
var videos: [String] = []
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = mynamesArray[indexPath.row]
tableView.reloadData()
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
refHandle = ref.child("Services").child("ServiceA").observe(.value, with : { (snapshot) in
mynamesArray = []
for child in snapshot.children {
mynamesArray.append((child as AnyObject).key)
}
print (mynamesArray)
})
return mynamesArray.count
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete {
mynamesArray.remove(at: indexPath.row)
tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "seque", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()
tableView.delegate = self
}
我是新手,所以我真的不知道堆栈溢出是如何工作的,如果我应该更新或添加任何内容,请告诉我:)
编辑:我忘了提这个,但我有第二个视图控制器,文本出现在其中:
import UIKit
import FirebaseDatabase
class ServicesDisplayViewController: UIViewController {
let ref = Database.database().reference()
@IBOutlet var descLabel: UILabel!
@IBOutlet var titlelabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
ref.child("ServiceC").child("Title").observeSingleEvent(of: .value, with: { DataSnapshot in
print(DataSnapshot) // replace this with textLabel or other item
})
titlelabel?.text = mynamesArray[myIndex]
descLabel?.text = descriptionList[myIndex]
// Do any additional setup after loading the view.
}
【问题讨论】:
-
从您的 cellForRowAt 方法中删除
tableView.reloadData(),同时确保您已设置委托和数据源 -
在tableView.delegate = self..之后添加tableView.dataSource = self..并把tableView.reloadData放在之后。
-
它们都不起作用
标签: ios swift firebase uitableview firebase-realtime-database