Home > OS >  display error of UILabel in tableview swift
display error of UILabel in tableview swift

Time:07-22

I currently have an error displaying my data in a tableview. I managed to display the cells, but there is not the exact number of cells displayed and the data is not displayed in it. How to display firebase data in UILabel? Thanks.

struct List {

   var heure: String?
   var date: String?
   var location: String?
   var event: String?
   var title: String?
   var comment: String?
       
       init(heure: String?, date: String?, location: String?, event: String?, title: String?, comment: String?) {
       self.heure = heure
       self.date = date
       self.location = location
       self.event = event
       self.title = title
       self.comment = comment
      }
   }

class FourthViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var Button: UIButton!
@IBOutlet weak var TableView: UITableView!

var activityList = [List]()
let userID = Auth.auth().currentUser?.uid


override func viewDidLoad() {
    super.viewDidLoad()
    let nib = UINib(nibName: "TableViewCell", bundle: nil)
    TableView.register(nib, forCellReuseIdentifier: "TableViewCell")
    TableView.dataSource = self
    TableView.delegate = self
    TableView.layer.cornerRadius = 20
    fetchActivityList()
    }

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return activityList.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

   let test = activityList[indexPath.row]
   cell.Title?.text = test.title
   cell.Heure?.text = test.heure
   cell.Date?.text = test.date
   cell.Location?.text = test.location
   cell.Event?.text = test.event
   cell.Comment?.text = test.comment

   return cell
  }


func fetchActivityList() {

   let ref = Database.database().reference()
   ref.child("Activities").child(userID!).observe(.childAdded, with: { (snapshot) in
               let results = snapshot.value as? [String : AnyObject]
               let titre = results?["Title"]
               let heure = results?["Heure"]
               let date = results?["Date"]
               let location = results?["Location"]
               let event = results?["EventType"]
               let comment = results?["Comment"]
       let myAct = List(heure: heure as! String?, date: date as! String?, location: location as! String?, event: event as! String?, title: titre as! String?, comment: comment as! String?)
               self.activityList.append(myAct)
               DispatchQueue.main.async {
                   self.TableView!.reloadData()
               }
          })
       }
   }

cell tableview

CodePudding user response:

Check your "fetchActivityList()" method by putting break points in it to verify if you are getting values in it. Check what values you are getting in snapshot and if all the optional variables have values.

CodePudding user response:

The problem is solved. I just changed these lines of code

 func fetchActivityList() {

   let ref = Database.database().reference()
   ref.child("Activities").child(userID!).observeSingleEvent(of: .value) { (snapshot) in
               let results = snapshot.value as? NSDictionary
               let titre = results?["Title"] as? String ?? "Pas de titre"
               let heure = results?["Heure"] as? String ?? "Pas d'heure"
               let date = results?["Date"] as? String ?? "Pas de Date"
               let location = results?["Location"] as? String ?? "Pas d'emplacement"
               let event = results?["EventType"] as? String ?? "Pas d'évènement"
               let comment = results?["TexField"] as? String ?? "Pas de commentaires"
       let myAct = List(heure: heure as String?, date: date as String?, location: location as String?, event: event as String?, title: titre as String?, comment: comment as String?)
               print(heure)
               print(titre)
               print(date)
               print(location)
               print(event)
               print(comment)
               self.activityList.append(myAct)
               DispatchQueue.main.async {
                   self.TableView!.reloadData()
                   }
              }
           }
       }
   
  • Related