swift – 如何检测最后一个单元格在UICollectionView中是否可见
发布时间:2020-12-14 02:27:54 所属栏目:百科 来源:网络整理
导读:我试图检测collectionView中的最后一个单元格是否可见. var isLastCellVisible: Bool { let lastIndexPath = NSIndexPath(forItem: self.messages.count - 1,inSection: 0) let visibleIndexPaths = self.collectionView.indexPathsForVisibleItems() let la
我试图检测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不会更改.
我在用什么
override func collectionView(collectionView: UICollectionView,willDisplayCell cell: UICollectionViewCell,forItemAtIndexPath indexPath: NSIndexPath) { if indexPath.row == dataSource.count - 1 { // Last cell is visible } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |