我试图检测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.
当collectionView向上滚动时,lastCellPositionY和contentHeight不会更改.
代替
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 } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。