加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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不会更改.
代替
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
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读