gpt4 book ai didi

ios - 无法将 firebase 数据库下载到我的应用程序

转载 作者:行者123 更新时间:2023-11-28 12:18:01 25 4
gpt4 key购买 nike

我是 swift 编程的新手。我正在尝试制作一个从 firebase 数据库下载学生数据的应用程序。我无法运行该应用程序。这是我的 JSON 文件:

{"classA":[
{
"name": "Student1",
"USN": "1DS16CS095"
},
{
"name": "student2",
"USN": "1DS16CS065"
},
{
"name":"student3",
"USN":"1DS16CS047"
}
]
}

这是我下载上述 JSON 文件并将其放入 tableView 的代码。 Modelstudent 是我的类(class),我在其中存储了我的变量名称和 USN。 marksstudentlistTableViewCell 是我用来操作原型(prototype)单元格标签的类。

import UIKit
import Firebase
import FirebaseDatabase

struct StudentData {
let StudentName : String
let StudentUSN : String
}

class marksstudentlist: UIViewController, UITableViewDelegate, UITableViewDataSource{

var FetchedStudentIDs = [StudentData]()

@IBOutlet weak var tableView: UITableView!
var ref: DatabaseReference!
var _selectedsub: Int!
var selectedsub: Int {
get {
return _selectedsub
}set {
_selectedsub = newValue
}
}

override func viewDidLoad() {
tableView.delegate = self
tableView.dataSource = self
if(classselected==0){
ref = Database.database().reference().child("classA")
ref.observe(DataEventType.value, with: { (snapshot) in
if snapshot.childrenCount>0{
for students in snapshot.children.allObjects as! [DataSnapshot]{
let studentObject = students.value as? [String: String]
let studentname = studentObject?["name"]
let studentUSN = studentObject?["USN"]
let student = Modelstudent(name: studentname , USN: studentUSN)
self.FetchedStudentIDs.insert(StudentData(StudentName :(studentname as? String!)! , StudentUSN : (studentUSN as? String!)! ) , at: 0)
}
self.tableView.reloadData()
}
})
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return FetchedStudentIDs.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "marksstudentlistcell", for: indexPath) as! marksstudentlistTableViewCell

cell.namelbl.text = FetchedStudentIDs[indexPath.row].StudentName// Student name comes from Firebase
cell.USNlbl.text = FetchedStudentIDs[indexPath.row].StudentUSN
return cell
}

@IBAction func backbtn(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}

最佳答案

使用 Swift Struct 是在 tableView 中获取 firebase 数据的最佳方式,

让我们开始吧

在你的 TableView swift 文件上,在类和东西上面粘贴这个

   Struct StudentData {
let StudentObject : String // If it's
let StudentName : String // It returns a string right?
let StudentUSN : String
// do the others.
}

好的,那么就在调用它的类下面创建一个 var

var FetchedStudentIDs = [StudentData]()

然后你就得到了firebase的阅读方法

     ref = Database.database().reference().child("classA")
ref.observe(DataEventType.value, with: { (snapshot) in
if snapshot.childrenCount>0{
self.studentslist.removeAll()
for students in snapshot.children.allObjects as! [DataSnapshot]{
let studentObject = students.value as? [String: AnyObject]
let studentname = studentObject?["name"]
let studentUSN = studentObject?["USN"]
let student = Modelstudent(name: studentname as! String?, USN: studentUSN as! String?) //storing in Modelstudent class
self.
FetchedStudentIDs.insert(StudentData(studentname:StudentName as! String , studentUSN : StudentUSN as! String ) , at: 0) // Student name comes from the struct Above, do the others as this
}
self.tableView.reloadData() // make sure you call this
}
})

返回你的tableView

    func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return FetchedStudentIDs.count
}

您的CellForRowAtIndexPath

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell

cell.exampleLabel.text = FetchedStudentIDs[Indexpath.row].StudentName// Student name comes from Firebase
return cell
}

希望对您有所帮助

关于ios - 无法将 firebase 数据库下载到我的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45784024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com