gpt4 book ai didi

ios - 显示含有少量数据的单元格背景颜色

转载 作者:行者123 更新时间:2023-11-30 12:43:31 24 4
gpt4 key购买 nike

我有一个 Collection View ,即使数据量很小,我也希望显示一些单元格。例如,在这张图片中,请注意只有 1 个项目,但所有其他单元格仍然显示背景颜色。

collectionView

我知道如何仅通过伪造用于填充 numberOfItemsInSection 中 Collection View 的数据量来做到这一点。这是我的示例代码:

class ViewController: UIViewController {

@IBOutlet weak var collectionView: UICollectionView!

let itemArray = ["1"]
}

extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
/// Note I can fake more cells here but don't think this is correct way.
return itemArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FavoritesCollectionViewCell

return cell
}
}

这只需要在屏幕上有足够的单元格显示时才起作用。例如,需要大约 8 个单元格来填充屏幕,一旦数组中有足够的项目来填充屏幕,就不再需要了。

有什么想法吗?

更新 1:

此 Collection View 将支持添加、删除和排序项目。不确定如果我在 numberOfItemsInSection

中添加虚假数据是否会出现问题

最佳答案

Note I can fake more cells here but don't think this is correct way

为什么不呢?如果您想要空单元格,请索要空单元格!这根本不是“假的”。

另一种方法是(实时)绘制一个看起来像空网格的网格,并将其显示为 Collection View 的背景。

关于ios - 显示含有少量数据的单元格背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41947458/

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