gpt4 book ai didi

swift - 如何检测最后一个单元格在 UICollectionView 中是否可见?

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:01 25 4
gpt4 key购买 nike

我正在尝试检测 collectionView 中的最后一个单元格是否可见。

var isLastCellVisible: Bool {

let lastIndexPath = NSIndexPath(forItem: self.messages.count - 1, inSection: 0)
let visibleIndexPaths = self.collectionView.indexPathsForVisibleItems()

let lastCellPositionY = self.collectionView.layoutAttributesForItemAtIndexPath(lastIndexPath)!.frame.origin.y
let bottomInset = self.collectionView.contentInset.bottom // changes when the keyboard is shown / hidden
let contentHeight = self.collectionView.contentSize.height

if visibleIndexPaths.contains(lastIndexPath) && (contentHeight - lastCellPositionY) > bottomInset {
return true
} else {
return false
}
}

什么有效:

如果最后一个单元格是可见的,并且显示了键盘,因此该单元格未被隐藏,则上述代码返回 true。如果它被隐藏,它返回 false。

但是当用户向上滚动并且最后一个单元格位于键盘上方时,我不知道如何返回 true。

lastCellPositionYcontentHeightcollectionView 向上滚动时不会改变。反而self.collectionView.bounds.origin.y 确实发生了变化,但我不知道如何将 lastCellPositionY 与它进行比较,因为它们的起源和 self.collectionView.bounds.origin.y 明显小于 lastCellPositionY

最佳答案

我在用什么

override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == dataSource.count - 1 {
// Last cell is visible
}
}

关于swift - 如何检测最后一个单元格在 UICollectionView 中是否可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37898209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com