繁体   English   中英

在tableview swift中显示UILabel的错误

[英]display error of UILabel in tableview swift

我目前在表格视图中显示我的数据时出错。 我设法显示了单元格,但没有显示确切的单元格数量,并且数据没有显示在其中。 如何在 UILabel 中显示 Firebase 数据? 谢谢。

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()
               }
          })
       }
   }

单元格表格视图

通过在其中放置断点来检查您的“ fetchActivityList() ”方法,以验证您是否在其中获取值。 检查您在快照中获得的值以及所有可选变量是否都有值。

问题已经解决了。 我只是更改了这些代码行

 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()
                   }
              }
           }
       }
   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM