【发布时间】:2017-07-17 00:10:49
【问题描述】:
尝试使用此方法在集合视图中加载数据
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if urls.count == 0 {
return UICollectionViewCell()
}
let url = urls[indexPath.item]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
for: indexPath) as! ImageCollectionViewCell
storageRef.reference(withPath: url).getData(maxSize: 15 * 1024 * 1024, completion: { data, error in
if let data = data {
cell.imageView.image = UIImage(data: data)
}
})
return cell
}
问题是 - 起初我可以看到两个没有数据的单元格,然后调用完成,我得到一个数据,但两个单元格中的第一个图像。 我怎样才能做对?我怎样才能同时获取元数据?
截图:
更新:
我写了一个数组
var datas = ["first", "second", "third", "fourth"]
将单元格代码更改为
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,
for: indexPath) as! ImageCollectionViewCell
cell.nameLabel.text = datas[indexPath.item]
return cell
}
得到了这个:
仍然看不出有什么问题。我的重写方法:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return datas.count / 2
}
已解决。应该返回 numberOfSections = 1
【问题讨论】:
-
你也可以添加截图吗?
-
@TalhaQ 我会,但我不认为它会有用
-
@TalhaQ,按要求
-
我相信你需要在collectionview范围之外使用这个storageRef.reference(withPath: url).getData。将数据保存在数组中并在此处填充
-
在函数中从 cellForItemAt 外部的存储中获取图像并在数组中接收所有图像,然后使用相应的数组在单元格中显示图像将解决您的问题
标签: ios swift firebase firebase-storage