我的问题是消息没有以正确的顺序显示,并且在我的设备/模拟器上的时间线 tableView 中,我发现每个单元格中显示了我的实际(在 Mac/设备上)日期和时间,但我想按照服务器上“createdat”列中的值创建的顺序获取消息。
不知道应该如何使用 orderByAscending
函数,所以我目前没有使用它。
如您所见,日期不匹配,并且消息的顺序错误,因为从未颠倒过它们
关于应用程序的一些信息:
从 ChannelsTVC
中的 prepareForSegue
中,我从数组中选择一个关键字,然后通过 filterToParse 将其“发送”到
变量,以便仅选择“Messages”类中与关键字相关的消息。MessagesTimelineTVC
在 PFQuery
//MARK: Retrive Date from Parse 1/3
我要求两者提供文本消息,它们分别日期,将它们放在两个不同的数组中,反向然后最终将它们显示在 tableView 中。
提前致谢。
在 MessagesTimelineTVC
cellForRowAtIndexPath
我有:
//MARK: Retrive Date from Parse 3/3
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
var useDate = dateFormatter.stringFromDate(self.messageDateArray[indexPath.row])
cell.dateMessageLabel.text = useDate
我通过 PFQuery 检索数据的函数位于 MessagesTimelineTVC
中的 viewDidAppear
中:
func loadData() {
timelineData.removeAll(keepCapacity: true) //erase previus contents
println("timelineData cleared")
//MARK: Starts the PFQuery
var findTimeLineDataQuery = PFQuery(className: "Messages")
findTimeLineDataQuery.whereKey("channel", containsString: FilterToParse) //MARK: - filter for Parse - should filter parse query
findTimeLineDataQuery.findObjectsInBackgroundWithBlock({
(objects : [AnyObject]?, error : NSError?) -> Void in
if error == nil {
for singleObject in objects! {
if let stringData = singleObject["message"] as? String {
self.timelineData.append(stringData)
//MARK: Retrive Date from Parse 1/3
if let messageDate = singleObject.createdAt {
self.messageDateArray.append(messageDate!)
println("retrived date from Parse is: *\(messageDate!)*")
}
}
}
let reversedArray : Array = self.timelineData.reverse() //MARK: Always Remember! (reverse array order)
self.timelineData = reversedArray as Array
//MARK: Retrive Date from Parse 2/3
let reversedMessageDateArray : Array = self.messageDateArray.reverse() //MARK: Always Remember! (reverse array order)
self.messageDateArray = reversedMessageDateArray as Array
self.tableView.reloadData()
}
})
}
最佳答案
您应该在查询变量中使用 orderByAscending
或 orderByDescending
,例如:
var query = PFQuery(className: "name")
query.orderByAscending("createdAt")
//You can use the functions like that
在你的情况下:
findTimeLineDataQuery.orderByAscending("createdAt")
这是最简单的方法。
关于ios - 无法按照通过 Parse.com 使用 createdAt 创建的对象的顺序获取对象 - Swift 1.2 - Xcode 6.4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32294596/