【发布时间】:2016-09-01 11:05:46
【问题描述】:
我正在尝试使用自定义单元格实现UICollectionView。
设置如下:
- 4 格
- 如果我获得下载图像的数据 => 用该图像填充单元格的 imageView。
- 否则:使用占位符。
图片的PFFiles保存在imageFileDic:[String:PFFile]内。
这是我的更新cellForItemAtIndexPath:
let collectionCell:SettingsCollectionViewCell =
collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell",
forIndexPath: indexPath) as! SettingsCollectionViewCell
if indexPath.row < imageFileDic.count {
if let imageId = imageFileIds[indexPath.row] as? String {
if let imageData = imageFileDic[imageId]{
collectionCell.collectionViewImage.file = imageData
collectionCell.collectionViewImage.loadInBackground()
}
}
} else{
collectionCell.collectionViewButton.setImage(UIImage(), forState: .Normal)
collectionCell.collectionViewImage.image = UIImage(named: "CameraBild.png")
}
现在有时(1/5 次)我的应用程序决定显示图像两次,或者在单元格 nr 的位置。 4.
在我的查询中,我总是在添加新数据之前删除字典和数组。
有什么想法吗?
编辑:
这是PFQuery我打电话:
let imageQuery = PFQuery(className: "Images")
imageQuery.whereKey("sender", equalTo: objectID!)
imageQuery.addAscendingOrder("createdAt")
imageQuery.cachePolicy = .NetworkElseCache
imageQuery.findObjectsInBackgroundWithBlock { (objects, error) in
if error != nil {
createAlert(error!)
}
else{
if let objects = objects{
self.imageFileDic.removeAll()
self.imageFileIds.removeAll()
for object in objects{
if let id = object.objectId{
print("id found")
if let imageFile = object.objectForKey("imageFile") as? PFFile{
self.imageFileIds.append(id)
self.imageFileDic[id] = imageFile
if object == objects.last{
print(self.imageFileIds.count)
print(self.imageFileDic.count)
self.mainCollectionView.reloadData()
}
}
}
}
}
}
}
}
【问题讨论】:
-
完全添加你的 cellForItemAtIndexPath 方法。我打赌你一开始不会做 collectionCell.collectionViewImage.image = nill
-
除了发布您的整个 cellForItemAtIndexPath 之外,还要发布您的查询代码并解释它如何处理异步响应。
-
我可以假设可重复使用的单元格不会为单元格项设置初始值。
-
@OlegGordiichuk 在我下载数据之前没有设置图像。而不是那个,它是一个空白的灰色空间。
-
@DuncanC 查询代码现已发布。 NetworkElseCache 是否有可能出错?
标签: ios swift uicollectionview pfimageview