从底部开始的Swift TableView(反向TableView)入门

3

是否可以倒置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] 反转数组即可解决问题。 - Khaledonia
4个回答

3

只需滚动到底部,每次插入单元格时都执行相同操作。

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

不要调转单元格的顺序:请向下滚动到最后。 - Stefano Vet
我以为你已经做了那个。只需在 cellForRowAtIndexPath() 中返回正确的单元格即可。 - Mundi
我再试一次,但还是不行。首先,我反转了tableview的数据,然后设置了一个变量来存储第一个indexpath,接着调用了scrollToRowAtIndexPath函数,但是tableview又回到了顶部。我再试一次..谢谢! - Stefano Vet

2

感谢解决方案,只需反转您的数组并像这样加载它:

var messagesArray = string

self.messagesArray = self.messageArray.reversed()

聊天应用程序

enter image description here


2
为了补充上面的回答,我将介绍使用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)

2

试试这个:

tableView.transform = CGAffineTransform(scaleX: 1, y: -1)

并且,在单元格内部:

transform = CGAffineTransform(scaleX: 1, y: -1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接