ios - Unable to download firebase database to my app -
i new programming in swift. trying make app downloads student data firebase database. unable app running. json file:
{"classa":[ { "name": "student1", "usn": "1ds16cs095" }, { "name": "student2", "usn": "1ds16cs065" }, { "name":"student3", "usn":"1ds16cs047" } ] }
this code download above json file , put in tableview. modelstudent class have variables name , usn stored. , marksstudentlisttableviewcell class using manipulate labels of prototype cell.
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 { { 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{ 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 resources can 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 firebase cell.usnlbl.text = fetchedstudentids[indexpath.row].studentusn return cell } @ibaction func backbtn(_ sender: any) { dismiss(animated: true, completion: nil) } }
using swift struct best way firebase data inside tableview,
let's started
on tableview swift file, above class , stuff paste this
struct studentdata { let studentobject : string // if it's let studentname : string // returns string right? let studentusn : string // others. }
okay create var down below class call it
var fetchedstudentids = [studentdata]()
then got reading method firebase
ref = database.database().reference().child("classa") ref.observe(dataeventtype.value, with: { (snapshot) in if snapshot.childrencount>0{ self.studentslist.removeall() 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 struct above, others } self.tableview.reloaddata() // make sure call } })
return tableview
func numberofsections(in tableview: uitableview) -> int { return 1 } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { return fetchedstudentids.count }
your 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 firebase return cell }
hope helps
Comments
Post a Comment