-1

I want to make the cells start from the bottom going up on the tableview. It is currently going from top to bottom. I am retrieving the data from firebase. How do I make it go otherwise (reverse it). Thanks!

var posts = [postStruct]()



override func viewDidLoad() {
    super.viewDidLoad()


    let ref = FIRDatabase.database().reference().child("Posts")

    ref.observeSingleEvent(of: .value, with: { snapshot in

        print(snapshot.childrenCount)

        for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {

            guard let value = rest.value as? Dictionary<String,Any> else { continue }

            guard let  title = value["Title"] as? String else { continue }

            let post = postStruct(title: title)

            self.posts.append(post)

        }

        self.tableView.reloadData()
    })
}




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

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

    let label1 = cell?.viewWithTag(1) as! UILabel
    label1.text = posts[indexPath.row].title
    return cell!
}

}

Riccardo
  • 289
  • 1
  • 4
  • 22
  • With `reversed`? For example, `self.posts = self.posts.reversed(); self.tableView.reloadData()` — But to be honest, it is quite unclear what you are asking to do, so perhaps that isn't it (whatever "it" is). – matt May 07 '17 at 18:53
  • See http://stackoverflow.com/questions/43336976/how-to-reverse-order-of-items-from-firebase, http://stackoverflow.com/questions/36589452/in-firebase-how-can-i-query-the-most-recent-10-child-nodes/36665442#36665442, http://stackoverflow.com/questions/37144030/how-do-you-properly-order-data-from-firebase-chronologically/37147850#37147850 and more from http://stackoverflow.com/search?q=%5Bfirebase%5D%5Bios%5D+reverse – Frank van Puffelen May 07 '17 at 20:15
  • It worked! Thanks Matt – Riccardo May 08 '17 at 23:55

1 Answers1

1

Try this :

self.posts.reversed().forEach {
    print($0)
}
KKRocks
  • 8,222
  • 1
  • 18
  • 84