将 UICollectionView 滚动到底部 [英] Scroll UICollectionView to bottom
本文介绍了将 UICollectionView 滚动到底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 UICollectionView 滚动到底部,以便最后一个项目在视图中.我曾尝试使用 scrollToItemAtIndexPath 但它似乎不起作用.我希望在通过 Parse.com 完成查询后发生这种情况
I would like to scroll the UICollectionView to the bottom so the last item is in the view. I have tried to use scrollToItemAtIndexPath but it does not seem to be working. I want this to happen after I have completed a query with Parse.com
谢谢
var query = PFQuery(className:"Chat")
// query.whereKey("user", equalTo:currentUser)
query.whereKey("rideId", equalTo:currentObjectId)
query.orderByDescending("createdAt")
query.includeKey("user")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
// The find succeeded.
NSLog("Successfully retrieved \(objects.count) scores.")
// Do something with the found objects
for object in objects {
NSLog("%@", object.objectId)
var testId = object.objectId
println(testId)
self.orderedIdArray.append(testId)
var message = object.objectForKey("message") as String
self.messageString = message
self.messageArray.append(self.messageString)
println(message)
var nameId = object.objectForKey("user") as PFUser
var username = nameId.username as String
self.nameString = username
self.namesArray.append(self.nameString)
println("username: \(username)")
self.collectionView?.reloadData()
}
} else {
// Log details of the failure
NSLog("Error: %@ %@", error, error.userInfo!)
}
NSLog("Ordered: %@", self.orderedIdArray)
}
推荐答案
我添加了以下几行以在查询完成后运行.
I have added the lines below to run once the query is complete.
var item = self.collectionView(self.collectionView!, numberOfItemsInSection: 0) - 1
var lastItemIndex = NSIndexPath(forItem: item, inSection: 0)
self.collectionView?.scrollToItemAtIndexPath(lastItemIndex, atScrollPosition: UICollectionViewScrollPosition.Top, animated: false)
更新到 Swift 5
let item = self.collectionView(self.collectionView, numberOfItemsInSection: 0) - 1
let lastItemIndex = IndexPath(item: item, section: 0)
self.collectionView.scrollToItem(at: lastItemIndex, at: .top, animated: true)
这篇关于将 UICollectionView 滚动到底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文