gpt4 book ai didi

ios - 有没有办法确定 self.collectionview.visiblecells 的顺序?

转载 作者:可可西里 更新时间:2023-11-01 05:02:16 68 4
gpt4 key购买 nike

在 Collection View 中,我想知道 Collection View 中显示的第一项。我想我会查看 visibleCells 并将成为列表中的第一项,但事实并非如此。

最佳答案

针对 Swift5 进行了更新

let visibleCells = self.collectionView.indexPathsForVisibleItems
.sorted { left, right -> Bool in
return left.section < right.section || (left.section == right.section && left.row < right.row)
}
.compactMap { [weak self] in indexPath -> UICollectionViewCell? in
return self?.collectionView.cellForItem(at: indexPath)
}

请注意,闭包中的任何 self 如果不弱化,都可以保持强引用。

let visibleCells = self.collectionView.indexPathsForVisibleItems
.sorted { $0.section < $1.section || ($0.section == $1.section && $0.row < $1.row) }
.flatMap { [weak self] in self?.collectionView.cellForItem(at: $0) }

Swift3

根据之前的回答,这里有一个 Swift3 等效于获取有序可见单元格,首先对可见索引路径进行排序,然后使用 sortedflatMap 获取 UICollectionViewCell。

let visibleCells = self.collectionView.indexPathsForVisibleItems
.sorted { left, right -> Bool in
return left.section < right.section || (left.section == right.section && left.row < right.row)
}.flatMap { [weak self] in indexPath -> UICollectionViewCell? in
return self?.collectionView.cellForItem(at: indexPath)
}

在更简化的版本中,可读性稍差

let visibleCells = self.collectionView.indexPathsForVisibleItems
.sorted { $0.section < $1.section || ($0.section == $1.section && $0.row < $1.row) }
.flatMap { [weak self] in self?.collectionView.cellForItem(at: $0) }

关于ios - 有没有办法确定 self.collectionview.visiblecells 的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30811306/

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