爲了補充上面的答案,我已經包含了使用Parse加載,加載和刷新的方法。或者,您可以用任何其他您喜歡的提取方法替換Parse。
首先負載
var query = PFQuery(className: "Comments")
query.orderByDescending("createdAt")
// limit the number of objects
query.limit = objectsPerPage
query.findObjectsInBackgroundWithBlock { (comments: [AnyObject]?, error: NSError?) -> Void in
// clean array if loading for the first time
self.objects.removeAll()
for eachComment in comments!
if self.objects.count < self.objectsPerPage
{
self.objects.append(eachComment as! PFObject)
}
}
// reverse the array
self.objects = self.objects.reverse()
self.tableView.reloadData()
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.objects.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
要裝入多個
// for the query
//skip already loaded objects
query.skip = self.objects.count;
// limit the number of objects
query.limit = objectsPerPage
// find the number of objects already loaded
let numberOfAlreadyLoadedObjects = self.objects.count
// add your query.findObjects here
// correct the order to update the array in the right order
self.objects = self.objects.reverse()
// add new posts
for eachComment in comments!
if (self.objects.count < self.objectsPerPage + numberOfAlreadyLoadedObjects)
{
self.objects.append(eachComment as! PFObject)
}
}
// reverse again
self.tableView.reloadData()
let lastIndex = NSIndexPath(forRow: self.objectsPerPage - 2 , inSection: 0)
self.tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: UITableViewScrollPosition.Top, animated: false)
要刷新
// skip already loaded objects
let numberOfAlreadyLoadedObjects = self.objects.count
query.skip = numberOfAlreadyLoadedObjects
// add your query.findObjects here
// correct the order
self.objects = self.objects.reverse()
// count the number of new objects found
let newObjectsFound = comments?.count
// append just the new ones
for eachComment in comments!
{
if self.objects.count < numberOfAlreadyLoadedObjects + newObjectsFound!
{
self.objects.append(eachComment as! PFObject)
}
}
// reverse
self.objects = self.objects.reverse()
self.tableView.reloadData()
let lastIndex = NSIndexPath(forRow: self.objects.count - 1, inSection: 0)
self.tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
'cell.setTitle.text = myArray.reversed()[ indexPath.row]'反轉你的數組將會訣竅。 – Alexander 2017-08-30 12:30:30