我正在从 JSON响应创建一个集合视图. Json响应具有高分辨率图像和大约2000年的图像数量.由于数据来自第三方,我无法控制图像大小. 执行: JSON示例: { "imagelist": [ { "largeImage": "https://am
执行:
JSON示例:
{
"imagelist": [
{
"largeImage": "https://amazingphotos/01.jpg"
},
{
"largeImage": "https://amazingphotos/02.jpg"
},
}
在UICollectionViewController中访问JSON:
Alamofire.request(.GET, dataURL)
.responseJSON { _, _, result in
if var json:JSON = JSON(result.value!){
self.imageURLs = json["imagelist"].arrayValue
self.collectionView?.reloadData()
}
}
在cellForItemAtIndexPath中,我将URL传递给单元格而不是图像本身.
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as! CollectionViewCell
cell.imageData = imageURLs[indexPath.row]
在自定义Cell我只是这样做:
var imageData:SwiftyJSON.JSON?{
didSet{
self.setup()
}
}
func setup(){
if let imageURLString = self.imageData?["largeImage"]{
let imageURL = NSURL(string: imageURLString.stringValue)
self.imageThumbnail.hnk_setImageFromURL(imageURL!)
}
问题:当我加载集合视图时,它会正确显示图像,一旦我开始滚动它就会在新单元格中显示我之前的图像几秒钟,然后刷新.
我多次使用集合视图但从未使用过如此多的数据.请指教.
TIA
override func prepareForReuse() {
myImageView.image = nil
super.prepareForReuse()
}
