为了补充上面的回答,我将介绍使用Parse进行加载、加载更多和刷新的方法。或者您可以用其他任何您喜欢的获取方法来替换Parse。
首次加载
var query = PFQuery(className: "Comments")
query.orderByDescending("createdAt")
query.limit = objectsPerPage
query.findObjectsInBackgroundWithBlock { (comments: [AnyObject]?, error: NSError?) -> Void in
self.objects.removeAll()
for eachComment in comments!
if self.objects.count < self.objectsPerPage
{
self.objects.append(eachComment as! PFObject)
}
}
self.objects = self.objects.reverse()
self.tableView.reloadData()
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.objects.count - 1, inSection: 0), atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
加载更多
query.skip = self.objects.count;
query.limit = objectsPerPage
let numberOfAlreadyLoadedObjects = self.objects.count
self.objects = self.objects.reverse()
for eachComment in comments!
if (self.objects.count < self.objectsPerPage + numberOfAlreadyLoadedObjects)
{
self.objects.append(eachComment as! PFObject)
}
}
self.tableView.reloadData()
let lastIndex = NSIndexPath(forRow: self.objectsPerPage - 2 , inSection: 0)
self.tableView.scrollToRowAtIndexPath(lastIndex, atScrollPosition: UITableViewScrollPosition.Top, animated: false)
刷新
let numberOfAlreadyLoadedObjects = self.objects.count
query.skip = numberOfAlreadyLoadedObjects
self.objects = self.objects.reverse()
let newObjectsFound = comments?.count
for eachComment in comments!
{
if self.objects.count < numberOfAlreadyLoadedObjects + newObjectsFound!
{
self.objects.append(eachComment as! PFObject)
}
}
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]
反转数组即可解决问题。 - Khaledonia