将 UICollectionView 滚动到底部
Posted
技术标签:
【中文标题】将 UICollectionView 滚动到底部【英文标题】:Scroll UICollectionView to bottom 【发布时间】:2014-12-03 16:49:58 【问题描述】:我想,以便最后一项在视图中。我曾尝试使用 scrollToItemAtIndexPath 但它似乎没有工作。我希望在完成对 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)
【问题讨论】:
您说scrollToItemAtIndexPath
“似乎不起作用”。它有什么作用?
我已将它放在self.collectionView?.reloadData()
行下,但在该行之后它不会运行任何东西。
这就是我正在使用的。这是正确的吗?let index = NSIndexPath(forItem:self.messageArray.count, inSection:0) self.collectionView?.scrollToItemAtIndexPath(index, atScrollPosition: UICollectionViewScrollPosition.Bottom, animated: true)
我的意思是我应该把代码放在...
所在的位置?
【参考方案1】:
我已添加以下行,以便在查询完成后运行。
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)
【讨论】:
你是说修复了吗?如果是这样,那就太好了!如果不是,那么我不确定为什么这是一个答案,而不是对您的问题的编辑。 另外,您是在查询块内执行此操作,紧跟在reloadData
之后吗?
它正在工作并且它在for
语句之外。
好的。我当时不明白最初的问题(因为你说你试过了,但没有用),但我想这并不重要:只要你解决了你的问题。
顺便说一句,reloadData
也应该在for
语句之外。【参考方案2】:
对于斯威夫特 -
水平滚动 - .right
垂直滚动 - .bottom
override func viewDidLayoutSubviews()
let section = 0
let lastItemIndex = self.dateCollectionView.numberOfItemsInSection(section) - 1
let indexPath:NSIndexPath = NSIndexPath.init(forItem: lastItemIndex, inSection: section)
self.dateCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .right, animated: false)
【讨论】:
【参考方案3】:适用于 Swift 3 和 多个部分
/**
This method scrolls the *UICollectionView* to the last item in the last section
*/
func scrollToLastItem()
let lastSection = collectionView.numberOfSections - 1
let lastRow = collectionView.numberOfItems(inSection: lastSection)
let indexPath = IndexPath(row: lastRow - 1, section: lastSection)
self.collectionView.scrollToItem(at: indexPath, at: .right, animated: true)
【讨论】:
【参考方案4】:为 Swift 3 更新:
let item = self.collectionView(self.collectionView!, numberOfItemsInSection: 0) - 1
let lastItemIndex = IndexPath(item: item, section: 0)
collectionView?.scrollToItem(at: lastItemIndex, at: UICollectionViewScrollPosition.top, animated: true)
【讨论】:
【参考方案5】:斯威夫特 4
collectionView?.scrollToItem(at: IndexPath(item: 3, section: 0), at: UICollectionViewScrollPosition.top, animated: false)
【讨论】:
【参考方案6】:斯威夫特 4:
用于水平滚动 - .right
垂直滚动 - .bottom
let lastItemIndex = self.collectionView.numberOfItems(inSection: 0) - 1
let indexPath:IndexPath = IndexPath(item: lastItemIndex, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .right, animated: false)
【讨论】:
【参考方案7】: lstMessages.reloadData()
let item = self.collectionView(lstMessages, numberOfItemsInSection: 0) - 1
let lastItemIndex = NSIndexPath(forItem: item, inSection: 0)
lstMessages.scrollToItemAtIndexPath(lastItemIndex, atScrollPosition: UICollectionViewScrollPosition.Top, animated: false)
lstMessages 是我的 collectionView
【讨论】:
【参考方案8】:Swift 3 更新
func viewScrollButton()
let lastItem = collectionView(self.collectionView!, numberOfRowsInSection: 0) - 1
let indexPath: NSIndexPath = NSIndexPath.init(item: lastItem, section: 0)
self.collectionView.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: false)
【讨论】:
【参考方案9】:斯威夫特 4.2
let lastSection = self.messagesCollectionView.numberOfSections - 1
let lastRow = self.messagesCollectionView.numberOfItems(inSection: lastSection)
let indexPath = IndexPath(row: lastRow - 1, section: lastSection)
self.messagesCollectionView.scrollToItem(at: indexPath, at: UICollectionView.ScrollPosition.top, animated: true)
【讨论】:
【参考方案10】:您可以跟踪可见视图的顶部和底部并加载下一页项目
func scrollViewDidScroll(_ scrollView: UIScrollView)
if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height))
print("bottom")
if (scrollView.contentOffset.y <= 0)
print("top")
【讨论】:
【参考方案11】:Swift 5 & 4 当你想显示collectionview的最后一个单元格时调用这个函数
//MARK:- Collection view Scroll to End
func funColvScrollToEnd()
let item = self.collectionView(self.colv!, numberOfItemsInSection: 0) - 1
let lastItemIndex = NSIndexPath(item: item, section: 0)
self.colv?.scrollToItem(at: lastItemIndex as IndexPath, at: .bottom, animated: false)
//MARK:- You can use .right/ .left/ .top/ .bottom
【讨论】:
【参考方案12】:这是我对 Swift5 的看法:
extension UICollectionView
public func scrollToLastItem(at scrollPosition: UICollectionView.ScrollPosition, adjustment: CGFloat = 0.0, withAdjustmentDuration duration: TimeInterval = 0.5)
let lastSection = self.numberOfSections - 1
let lastRowInLastSection = self.numberOfItems(inSection: lastSection)
if lastSection > 0, lastRowInLastSection > 0
let indexPath = IndexPath(row: lastRowInLastSection - 1, section: lastSection)
let visibleIndexPaths = self.indexPathsForVisibleItems
if !visibleIndexPaths.contains(indexPath)
self.self.scrollToItem(at: indexPath, at: scrollPosition, animated: true)
UIView.animate(withDuration: duration)
switch scrollPosition
case .top, .bottom, .centeredVertically:
self.contentOffset.y += adjustment
case .left, .right, .centeredHorizontally:
self.contentOffset.x += adjustment
default:
print("Inavlid scrollPosition: \(scrollPosition)")
【讨论】:
【参考方案13】:Swift 5 如果有水平动画
func scrollToIndex(index:Int)
self.collectionView?.scrollToItem(at: IndexPath(item: index, section: 0), at: .centeredHorizontally, animated: true)
【讨论】:
以上是关于将 UICollectionView 滚动到底部的主要内容,如果未能解决你的问题,请参考以下文章