swift start tableView from bottom (reverse tableVi

2019-03-22 06:52发布

问题:

Is it possible to reverse order a tableView? I search a lot for a solution but nothing works.

The effect is like whatsapp chat.

A normal tableView is:

----------
label 1
----------
label 2
----------
label 3
----------
empty
----------
empty
----------

scroll direction
   |
   |
   v

Desired result:

----------
empty
----------
empty
----------
empty
----------
empty
----------
label 3
----------
label 2
----------
label 1
----------

 scroll direction
      ^
      |
      |

Thanks for help

回答1:

Just scroll to the end and do the same whenever you insert a cell.

tableView.scrollToRowAtIndexPath(bottomIndexPath, atScrollPosition: .Bottom,
          animated: true)


回答2:

To complement the answer above I've included the methods to load, load more and refresh using Parse. Or you can replace Parse by any other fetch method you like.

First load

    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)

To load more

    // 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)

To refresh

    // 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)