提问人:huisoo 提问时间:11/10/2023 更新时间:11/10/2023 访问量:8
为什么特定单元格在可见单元格之间显示EndDistried的原因
Why specific cell didEndDisplaying between visible cells
问:
如果滚动到 collectionView,则特定单元格将隐藏在可见单元格中 我希望那个单元格不要隐藏
我不知道为什么会这样。
macOS 索诺玛 14.0 Xcode代码15.0.1 iOS的17.0.1
视频捕获 在此处输入图像描述
这是我的代码
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func setupCollectionView() {
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
self.colors.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.frame.width - 40
let height = 100.0
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
let height = 100.0
return -height * 0.6
}
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let first = collectionView.indexPathsForVisibleItems.sorted(by: {
($0.section, $0.item) < ($1.section, $1.item)
}).first,
let last = collectionView.indexPathsForVisibleItems.sorted(by: {
($0.section, $0.item) < ($1.section, $1.item)
}).last else { return }
print("\(first) < \(indexPath) < \(last)")
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.contentsView.backgroundColor = self.colors[indexPath.item]
cell.layer.zPosition = CGFloat(indexPath.section) * 1000.0 + CGFloat(indexPath.row)
return cell
}
}
结果 打印 在此处输入图像描述
要 不隐藏,可见
尝试 1
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let first = collectionView.indexPathsForVisibleItems.sorted(by: {
($0.section, $0.item) < ($1.section, $1.item)
}).first,
let last = collectionView.indexPathsForVisibleItems.sorted(by: {
($0.section, $0.item) < ($1.section, $1.item)
}).last else { return }
if first.section <= indexPath.section && last.section >= indexPath.section,
first.item <= indexPath.item && last.item >= indexPath.item {
cell.isHidden = false
}
}
结果
- 什么都不会发生
尝试 2
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let first = collectionView.indexPathsForVisibleItems.sorted(by: { ($0.section, $0.item) < ($1.section, $1.item)}).first,
let last = collectionView.indexPathsForVisibleItems.sorted(by: { ($0.section, $0.item) < ($1.section, $1.item)}).last else { return }
if first.section <= indexPath.section && last.section >= indexPath.section,
first.item <= indexPath.item && last.item >= indexPath.item {
self.collectionView.reloadItems(at: [indexPath])
}
}
结果
- 重复隐藏和显示
答: 暂无答案
评论