2014-10-01 125 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)