显示少量数据的单元格背景颜色
Show cell background color with small amounts of data
我有一个集合视图,我希望即使数据量很小也能显示一些单元格。例如,在这张图片中,请注意只有 1 个项目,但所有其他单元格仍以背景色显示。
我知道如何通过伪造用于填充 numberOfItemsInSection
中的集合视图的数据量来做到这一点。这是我的示例代码:
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:
此集合视图将支持添加、删除和排序项目。如果我在 numberOfItemsInSection
中添加虚假数据,不确定这是否会成为问题
Note I can fake more cells here but don't think this is correct way
为什么不呢?如果您想要空单元格,请索要空单元格!根本不是 "faking"。
另一种方法是(实时)绘制一个看起来像空网格的网格,并将其显示为集合视图的背景。
我会做类似的事情:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return max(itemArray.count, 8)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FavoritesCollectionViewCell
if (indexPath.row <= itemArray.count) {
// just guessing here how your setting image
cell.image = itemArray[indexPath.row].image
}
return cell
}
我认为你走在正确的轨道上,这不是假的 - 你只是在设置单元格并设置元素的最小数量
我有一个集合视图,我希望即使数据量很小也能显示一些单元格。例如,在这张图片中,请注意只有 1 个项目,但所有其他单元格仍以背景色显示。
我知道如何通过伪造用于填充 numberOfItemsInSection
中的集合视图的数据量来做到这一点。这是我的示例代码:
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:
此集合视图将支持添加、删除和排序项目。如果我在 numberOfItemsInSection
Note I can fake more cells here but don't think this is correct way
为什么不呢?如果您想要空单元格,请索要空单元格!根本不是 "faking"。
另一种方法是(实时)绘制一个看起来像空网格的网格,并将其显示为集合视图的背景。
我会做类似的事情:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return max(itemArray.count, 8)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FavoritesCollectionViewCell
if (indexPath.row <= itemArray.count) {
// just guessing here how your setting image
cell.image = itemArray[indexPath.row].image
}
return cell
}
我认为你走在正确的轨道上,这不是假的 - 你只是在设置单元格并设置元素的最小数量