是否有可能倒序一个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
^
|
|谢谢你的帮助
发布于 2014-10-01 18:49:05
只要滚动到末尾,每次插入单元格时都执行同样的操作。
tableView.scrollToRowAtIndexPath(bottomIndexPath, atScrollPosition: .Bottom,
animated: true)发布于 2015-12-13 20:52:54
为了补充上面的答案,我包含了使用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)发布于 2020-03-19 07:47:00
解决方案是只找到反向数组并加载它,如下所示:
变量messagesArray =字符串
self.messagesArray = self.messageArray.reversed()
聊天应用程序

https://stackoverflow.com/questions/26148367
复制相似问题