2014-10-01 122 views
2

是否有可能顛倒一個tableView?我搜索了很多的解決方案,但沒有任何工作。快速啓動tableView從底部(反向tableView)

效果就像whatsapp聊天。

正常的tableView是:

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

scroll direction 
    | 
    | 
    v 

期望的結果:

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

scroll direction 
    ^
     | 
     | 

感謝您的幫助

+1

'cell.setTitle.text = myArray.reversed()[ indexPath.row]'反轉你的數組將會訣竅。 – Alexander 2017-08-30 12:30:30

回答

1

只需滾動到結束,只要你插入一個細胞做同樣的。

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

滾動到最後不要顛倒單元格的順序:( – 2014-10-01 19:00:17

+0

我以爲你已經這樣做了,只需在'cellForRowAtIndexPath()'中返回正確的單元格。 – Mundi 2014-10-01 19:09:46

+0

我再試一次,但不起作用。 tableview的數據然後我設置一個var與第一個索引路徑,然後我調用scrollToRowAtIndexPath的func但tableview staty再次。我再試一次..謝謝! – 2014-10-01 19:37:21

0

爲了補充上面的答案,我已經包含了使用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)