Swift / Firebase - 按日期排序tableview中的帖子 [英] Swift/Firebase - Sort posts in tableview by date
问题描述
在此先感谢。
$ b $
root
自动识别
发送者
信息
时间戳
用户
UID
用户名
这个问题有点儿不清楚,但是如果你想填充一个tableView,你将需要从Firebase获取数据,填充一个数组(dataSource),并重新加载tableView。
<首先,我们需要设置一个.ChildAdded观察者来将数据加载到用作tableView数据源的messagesArray中。
class AppDelegate:NSObject,NSApplicationDelegate {
var messagesArray = [[String:String]]()//一个数组字段,tableView数据源
var initialLoad = true
以及加载数据ini的代码然后观察添加的消息
messagesRef.observeEventType(.ChildAdded,withBlock:{snapshot in
让dict = [String:String]()
dict [date] = snapshot.value(date)
dict [msg] = snapshot.value(message)
dict [sender] = snapshot.value(sender)
self.messagesArray.append(dict)
if(self.initialLoad == false ){//在第一次加载时,不要重载tableView,直到所有的孩子被加载
self.itemsTableView.reloadData()
}
})
$ c $那么 - 这是很酷的部分...
$ b code> //这个.Value事件会在子添加事件后触发,重新加载tableView
//第一次设置后续的childAdded事件,在每个子
//被添加后加载在将来
messagesRef.observeSingleEventOfType(.Value,withBlock:{snapshot in
print(inital data loaded so reload tabl EVIEW! \(snapshot.childrenCount))
self.messagesTableView.reloadData()
self.initialLoad = false
})
使用上面的代码,数据的顺序是通过它们的键 - 如果这些键是由Firebase生成的,它们将按顺序创建消息。
然后从messagesArray填充tableView,然后您可以选择将日期,消息和发件人放入tableView列,或者您希望填充cellView。
另一个要求是让它们按Date排序,这是一个超级好的问题,在这里有答案 - 这是一个两部分的问题,但你会看到这个想法的过程。
也想要利用Firebases查询功能来获取你想要的具体数据,而不是从上面的一般数据...
$ p $ messagesRef.queryOrderedByChild (date)。observeEventType(.ChildAdded,withBlock:{
快照//从快照创建一个字典并添加到tableview
})
Basically I am trying to make a twitter-like app, showing status updates of the people you follow, ordered by date, just like twitter. How can I do that? I have the code for displaying the posts in a table view, but the ordering is all messed up and looks quite random. I have a path called posts, with an autoID path, containing message, date and sender. How is this possible, without just showing the date in order from new > old, but also show the message and sender as well?
Thanks in advance.
root
posts
autoID
sender
message
timestamp
users
UID
username
The question is little bit unclear but if you want to populate a tableView you will need to get the data from Firebase, populate an array (the dataSource), and reload the tableView.
First off we need to set up an .ChildAdded observer to load the data into the messagesArray, which is used as the tableView datasource
class AppDelegate: NSObject, NSApplicationDelegate {
var messagesArray = [[String:String]]() //an array of dicts, tableView datasource
var initialLoad = true
and the code to load the data initially and then watch for added messages
messagesRef.observeEventType(.ChildAdded, withBlock: { snapshot in
let dict = [String: String]()
dict["date"] = snapshot.value("date")
dict["msg"] = snapshot.value("message")
dict["sender"] = snapshot.value("sender")
self.messagesArray.append(dict)
if ( self.initialLoad == false ) { //upon first load, don't reload the tableView until all children are loaded
self.itemsTableView.reloadData()
}
})
then - and this is the cool part...
//this .Value event will fire AFTER the child added events to reload the tableView
// the first time and to set subsequent childAdded events to load after each child
// is added in the future
messagesRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
print("inital data loaded so reload tableView! \(snapshot.childrenCount)")
self.messagesTableView.reloadData()
self.initialLoad = false
})
With the above code, the order of the data is by their key - and if those keys were generated by Firebase, they will be sequential in the order the messages were created.
Your tableView is then populated from the messagesArray, and you can pick off the date, message and sender to put into the tableView columns or however you want the populate your cellView.
Your other requirement was to have them ordered by Date, descending. That's a super great question and had has answer here - it was a two part question but you'll see the thought process.
In Firebase, how can I query the most recent 10 child nodes?
you will also want to leverage Firebases query capabilities to get the specific data you want instead of the general data from above...
messagesRef.queryOrderedByChild("date").observeEventType(.ChildAdded, withBlock: {
snapshot in
//create a dict from the snapshot and add to tableview
})
这篇关于Swift / Firebase - 按日期排序tableview中的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!