作者:mobiledu2502930533 | 来源:互联网 | 2023-05-19 06:21
Iamnewatswift4programming.Itrytoshowsomedata(actuallytheyarechildnames)fromdatabas
I am new at swift 4 programming. I try to show some data(actually they are child names) from database of firebase. When view did load, I can take them and show on tableView but it is not working real time. When I delete some data from firebase, I need to refresh(go back and load again) the viewController. If I refresh the tableView with timer, all data append from its end. If I, firstly, delete after append new data to tableView. It is jittering. Is there anyway to synchronize this tableView with firebase, without these problems?
我是swift 4编程的新手。我尝试从firebase数据库中显示一些数据(实际上它们是子名称)。当视图加载时,我可以将它们显示在tableView上,但它不能实时工作。当我从firebase中删除一些数据时,我需要刷新(返回并重新加载)viewController。如果我用计时器刷新tableView,则所有数据都从其末尾追加。如果我,首先在将新数据附加到tableView后删除。它在抖动。反正有没有同步这个tableView与firebase,没有这些问题?
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
ref.keepSynced(true)
let userRef = self.ref.child("Users").child(Username).child("Schedule")
userRef.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
self.myList.append(key)
self.CourseList.append(key)
self.LessonsTableView.reloadData()
}
// Lessons are taking and after, they are locating under phone's memory!
UserDefaults.standard.set(self.CourseList, forKey: "LessonsArray")
UserDefaults.standard.synchronize()
})
}
//Setting up our table
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = LessonsTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LessonsTableViewCell
cell.myLabell.text = myList[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let Dummy:String = myList[indexPath.row] as Any as! String
let Selected:String = (Dummy as NSString) as Any as! String
UserDefaults.standard.set(Selected, forKey: "Selected")
self.performSegue(withIdentifier: "NextPage", sender: self)
}
2 个解决方案