Home Swift display data from firebase correctly
Reply: 0

Swift display data from firebase correctly

NewYorker
1#
NewYorker Published in 2017-12-07 19:12:21Z

First, excuse me for my english. I do not get the data from Firebase displayed correctly in my tableview. I've already tried everything.

My function:

class Diary: UITableViewController{

      var postArrayDate = [String]()
      var postArrayValue = [Post]()
...
...

func loadTableview(){
    let userID = Firebase.Auth.auth().currentUser?.uid

    databaseRef = Database.database().reference().child("DiIOS").child(userID!)
    databaseRef.observe(.value, with: {(snapshot) in

        var newItems = [Post]()
        var newItemsDate = [String]()


        for year in snapshot.children.allObjects as! [DataSnapshot] {
            for month in year.children.allObjects as! [DataSnapshot] {
                for day in month.children.allObjects as! [DataSnapshot] {
                    for date in day.children.allObjects as! [DataSnapshot] {
                        for time in date.children.allObjects as! [DataSnapshot] {
                            let newPost = Post(snapshot: time)
                            newItems.append(newPost)
                            let newDate = Post(snapshot: time)
                            newItemsDate.append(newDate.snap6Date!)
                        }
                    }
                }
            }
        }
        self.postArrayValue = newItems
        self.postArrayDate = newItemsDate
        self.postArrayDate = self.uniq(source: self.postArrayDate)

        print(self.postArrayDate)
        self.tableView.reloadData()

    }){
        (error) in
        print(error.localizedDescription)
    }

}

I want to have a header with the date that works.

override func numberOfSections(in tableView: UITableView) -> Int {
    return postArrayDate.count
}


override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return heightOfHeader
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = Bundle.main.loadNibNamed("TableViewCell1", owner: self, options: nil)?.first as! TableViewCell1

    headerView.date.text = postArrayDate[section]
    headerView.backgroundColor = UIColor.white

    return headerView
}

My Firebase looks like this:

enter code here
{
"DiIOS" : {
"N9*********iy12" : {
  "2017" : {
    "December" : {
      "06 Wednesday" : {
        "Wednesday, December 06, 2017" : {
          "6:50 PM" : {
            "basalInsulin" : "-",
            "blutdruck" : "-",
            "blutzucker" : "-",
            "bolusInsulin" : "-",
            "date" : "Wednesday, December 06, 2017",
            "day" : "06 Wednesday",
            "ke" : "-",
            "notiz" : "-",
            "puls" : "-",
            "snap1DiabetesIOS" : "DiabetesIOS",
            "snap2UserID" : "N9iYKMU1vTfCzsV2MHYXbqTeiy12",
            "snap3Year" : "2017",
            "snap4Month" : "December",
            "snap5Day" : "06 Wednesday",
            "snap6Date" : "Wednesday, December 06, 2017",
            "snap7Time" : "6:50 PM",
            "time" : "6:50 PM"
          }
        }
      },
      "07 Thursday" : {
        "Thursday, December 07, 2017" : {
          "6:50 PM" : {
            "ba" : "-",
            "bl" : "-",
            "b" : "-",
            "bo" : "-",
            "date" : "Thursday, December 07, 2017",
            "day" : "07 Thursday",
            "k" : "-",
            "notiz" : "-",
            "p" : "-",
            "snap1" : "DiIOS",
            "snap2UserID" : "N9******y12",
            "snap3Year" : "2017",
            "snap4Month" : "December",
            "snap5Day" : "07 Thursday",
            "snap6Date" : "Thursday, December 07, 2017",
            "snap7Time" : "6:50 PM",
            "time" : "6:50 PM"
          },
          "7:41 PM" : {
            "ba" : "-",
            "bl" : "-",
            "b" : "-",
            "bo" : "-",
            "date" : "Thursday, December 07, 2017",
            "day" : "07 Thursday",
            "k" : "-",
            "notiz" : "-",
            "p" : "-",
            "snap1" : "DiIOS",
            "snap2UserID" : "N9**********y12",
            "snap3Year" : "2017",
            "snap4Month" : "December",
            "snap5Day" : "07 Thursday",
            "snap6Date" : "Thursday, December 07, 2017",
            "snap7Time" : "7:41 PM",
            "time" : "7:41 PM"
          }
        }
      }
    }
  }
}
}
}

And now I want to get everything under the same date, displayed in the tableview under the same header (section).

I show my tableview always under each header everything that I have saved. that is wrong. in this example he can only show one thing under header "Wednesday, December 06, 2017" and two things under header "Thursday, December 07, 2017".

My UITableviewCell:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return postArrayValue.count
}

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

    let cell2 = Bundle.main.loadNibNamed("TableViewCell2", owner: self, options: nil)?.last as! TableViewCell2

        cell2.time.text = postArrayValue[indexPath.row].time
        cell2.bl.text = postArrayValue[indexPath.row].bl
        cell2.k.text = postArrayValue[indexPath.row].k
        cell2.bo.text = postArrayValue[indexPath.row].bo
        cell2.b.text = postArrayValue[indexPath.row].b
        cell2.bl = postArrayValue[indexPath.row].bl
        cell2.p.text = postArrayValue[indexPath.row].p
        cell2.notiz.text = postArrayValue[indexPath.row].notiz
        return cell2

        return UITableViewCell()

}

This is my Post file:

import Foundation
import Firebase

struct Post {

var date: String!
var time: String!
var day: String!
var bl: String!
var k: String!
var bo: String!
var ba: String!
var b: String!
var p: String!
var notiz: String!
var ref: DatabaseReference!
var key: String!

var snap1DiIOS: String!
var snap2UserID: String!
var snap3Year: String!
var snap4Month: String!
var snap5Day: String!
var snap6Date: String!
var snap7Time: String!

init(date: String, time: String, day: String, bl: String, k: String, bo: String, ba: String, b: String, p: String, notiz: String, key: String = "", snap1DiIOS: String, snap2UserID: String, snap3Year: String, snap4Month: String, snap5Day: String, snap6Date: String, snap7Time: String) {
    self.date = date
    self.time = time
    self.day = day
    self.bl = bl
    self.k = k
    self.bo = bo
    self.ba = ba
    self.b = b
    self.p = p
    self.notiz = notiz
    self.key = key
    self.ref = Database.database().reference()

    self.snap1DiIOS = snap1DiIOS
    self.snap2UserID = snap2UserID
    self.snap3Year = snap3Year
    self.snap4Month = snap4Month
    self.snap5Day = snap5Day
    self.snap6Date = snap6Date
    self.snap7Time = snap7Time
}

init(snapshot: DataSnapshot) {

    let snapshotValue = snapshot.value as? NSDictionary

    self.date = snapshotValue!["date"] as? String
    self.time = snapshotValue!["time"] as? String
    self.day = snapshotValue!["day"] as? String
    self.bl = snapshotValue!["bl"] as? String
    self.k = snapshotValue!["k"] as? String
    self.bo = snapshotValue!["bo"] as? String
    self.ba = snapshotValue!["ba"] as? String
    self.b = snapshotValue!["b"] as? String
    self.p = snapshotValue!["p"] as? String
    self.notiz = snapshotValue!["notiz"] as? String
    self.key = snapshot.key
    self.ref = snapshot.ref

    self.snap1DiIOS = snapshotValue!["snap1DiIOS"] as? String
    self.snap2UserID = snapshotValue!["snap2UserID"] as? String
    self.snap3Year = snapshotValue!["snap3Year"] as? String
    self.snap4Month = snapshotValue!["snap4Month"] as? String
    self.snap5Day = snapshotValue!["snap5Day"] as? String
    self.snap6Date = snapshotValue!["snap6Date"] as? String
    self.snap7Time = snapshotValue!["snap7Time"] as? String
}


func toAnyObject() -> [String: AnyObject] {
    return ["date": date as AnyObject, "time": time as AnyObject, "day": day as AnyObject, "bl": bl as AnyObject, "k": k as AnyObject, "bo": bo as AnyObject, "ba": ba as AnyObject, "b": b as AnyObject, "p": p as AnyObject, "notiz": notiz as AnyObject, "snap1DiIOS": snap1DiIOS as AnyObject, "snap2UserID": snap2UserID as AnyObject, "snap3Year": snap3Year as AnyObject, "snap4Month": snap4Month as AnyObject, "snap5Day": snap5Day as AnyObject, "snap6Date": snap6Date as AnyObject, "snap7Time": snap7Time as AnyObject]
}

}

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304197 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO