我建立,在一个UICollectionView显示GIF文件的应用程序是空白。我使用Alamofire以及FlipBoard的FLAnimatedImage。我得到一个gif网址列表,然后设置获取的图像。只有一个UICollectionViewController细胞正常工作,别人
当我运行此,我仅第一页上的第一个单元格是否正常工作。当我滚动到第二页时,同一个(重复使用的单元格)是唯一可以工作的单元格。我通过打印单元格的内存地址来检查它,它是唯一显示正确图像的单元。
我不认为这是FLAnimatedImage一个问题,我试图用正常的UIImage的,它仍然有这种行为。我怀疑它必须处理我的UICollectionViewCell的子类。在我的故事板中,我试着引用我的UICollectionViewCell的子类,并且在界面中没有UICollectionViewCell。
的Gif数据模型
class GifData : NSObject {
let id : String
let url : String
init(id: String, url: String) {
self.id = id
self.url = url
}}
集合视图控制器
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView!.registerClass(MyCollectionViewCell.self, forCellWithReuseIdentifier: myReuseIdentifier)
Alamofire.request(.GET, "http://api.giphy.com/v1/gifs/search",
parameters: ["q":"hello", "api_key": "dc6zaTOxFJmzC", "limit" : "29"])
.responseJSON { response in
if let JSON = response.result.value {
let gifData = (JSON.valueForKey("data") as! [NSDictionary])
.map {
GifData(id: $0["id"] as! String, url: $0["images"]!["fixed_width_small_still"]!!["url"] as! String)
}
self.gifUrls.addObjectsFromArray(gifData)
self.collectionView?.reloadData()
}
}
}
集合视图数据源
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return gifUrls.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cakeReuseIdentifier, forIndexPath: indexPath) as! CakeCollectionViewCell
if (self.gifUrls.count != 0) {
Alamofire.request(.GET, (self.gifUrls.objectAtIndex(indexPath.row) as! GifData).url)
.response(completionHandler: { (request, response, data, error) -> Void in
cell.animatedImageView.animatedImage = FLAnimatedImage(animatedGIFData: data!)
})
return cell
}
return cell
}
我CollectionViewCell
class CakeCollectionViewCell: UICollectionViewCell {
var animatedImageView = FLAnimatedImageView()
override init(frame: CGRect) {
super.init(frame: frame)
self.animatedImageView.frame = self.frame
self.contentView.addSubview(animatedImageView)
self.contentView.clipsToBounds = true
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForReuse() {
super.prepareForReuse()
self.animatedImageView.animatedImage = nil
}}
不幸的是,自动调整面具没有工作,但我设法通过不使用UICollectionViewController来解决这个问题,只是使用普通的UIViewController和手动添加集合视图自己。出于某种原因,这个实现使用了我之前使用的相同代码。 ...对于你提到的问题,我最终从api查询中获取GET请求后,只需将这些gif数据设置为带有相应索引的NSMapTable。然后,在我的collectionDataSource中,如果NSMapTable的索引不为零,则设置图像。这是否更好? – lawrence
奇数。我很好奇并且重复了你的项目......即使我在调用'reloadData()'之前预先加载了所有gif,图像也无法显示。奇怪的......哦,好吧......无论如何,在普通的视图控制器中有一个集合视图更为灵活。至于另一个问题,(1)地图表是Swift的一个奇怪的选择,为什么不使用字典呢? (2)即使有了这个想法,在其图像之前出现的单元格也不会显示它们的图像。我建议你将这个问题转移到另一个问题上,而不是在评论中试图谈论它。 –