0

I am fairly new to realm of coding and I know this question might have been answered similarly elsewhere but when it comes to Firebase it may not. I was following this answer (Adding sections, separated by dates, to UITableView in Swift) but encounters complication when I implement .ChildAdded and insertRowAtIndexPath. My code as follows:

var ref: FIRDatabaseReference!
var mileageDatabase: [FIRDataSnapshot]! = []
var retrieveDates: [(String)] = []
var uniqueDates: [(String)] = []

struct tripDataset {
    let date: String
    let purpose: String
}
var tripItems = Dictionary<String, Array<tripDataset>>()

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView = UITableView(frame: self.tableView.frame, style: .Grouped)

    let nib = UINib(nibName: "TripTableViewCell", bundle: nil)
    self.tableView.registerNib(nib, forCellReuseIdentifier: "cell")
    configureDatabase()

}

func configureDatabase() {
    self.ref = FIRDatabase.database().reference()
    if let user = FIRAuth.auth()?.currentUser {
        self.ref.child("mileage").queryOrderedByChild("user").queryEqualToValue(user.uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in
            self.mileageDatabase.insert(snapshot, atIndex: 0)

            let snapValues = snapshot.value as! Dictionary< String, AnyObject>
            let snapDates = snapValues["date"] as! String
            let snapPurpose = snapValues["purpose"] as! String

            self.retrieveDates.append(snapValues["date"] as! String)
            self.uniqueDates = Array(Set(self.retrieveDates))

            if self.tripItems.indexForKey(snapDates) == nil {
                self.tripItems[snapDates] = [tripDataset(date: snapDates, purpose: snapPurpose)]
            } else {
                self.tripItems[snapDates]!.append(tripDataset(date: snapDates, purpose: snapPurpose))
            }

            self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Automatic) //Stuck hereeee.....

        })
    }        
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.uniqueDates.count 
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.tripItems[section].count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TripTableViewCell

    cell.dateTimeLabel.text = self.uniqueDates(indexPath.section)
    cell.purposeLabel.text = self.tripItem[self.uniqueDates(indexPath.section)]["purpose"]

    return cell
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.uniqueDates(section)
}

I know there are many errors here, is there anyone who can direct me accordingly? All the async nature of Firebase just complicates the flow for me.

Community
  • 1
  • 1
Koh
  • 2,687
  • 1
  • 22
  • 62
  • What are the issues and what are you trying to do..? – Dravidian Sep 25 '16 at 09:59
  • @Dravidian sort tableview by dates, where each day is one section itself. The idea is to group all trips within a day in one section. – Koh Sep 25 '16 at 10:42

1 Answers1

0

try this . Change below code from

self.retrieveDates.append(snapValues["date"] as! String)

to

self.retrieveDates.insert( snapValues, at : 0)
Habib Alejalil
  • 435
  • 1
  • 4
  • 17