尝试使用此方法在集合视图中加载数据
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
}
事情是这样的--首先我可以看到两个单元格都没有数据,然后调用completion,我得到一个数据,但是两个单元格中的第一个图像。我怎么能做对呢?我怎么能同时获得元数据呢?
截图:
UPD:
我写了一个数组
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
}
2条答案
按热度按时间yv5phkfx1#
我认为问题在于您正在下载 cellForItemAt indexPath: 函数中的信息,而您应该像这样在另一个函数中下载图像
jecbmhm32#
numberOfSection
函数应返回1