0

我正在使用 UICollectionView 制作像 AppStore 这样的应用程序,所以从 url 下载数据时

import Alamofire
import AlamofireImage

class AppCategory: NSObject {
var id: NSNumber?
var title: String?
var apps: [App]?
var type: NSNumber?

static func fetchFeaturedApps(completionHandler: @escaping ([AppCategory]) -> ()) {

let jsonResult = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String: AnyObject]
var appCategories = [AppCategory]()
             let sections = OFFERIM["sections"] as! [Dictionary<String, AnyObject>]
            for i in 0..<sections.count {
                var appCategory = AppCategory()
                var apps = [App]()

                let sectionTitle = sections[i]["title"] as! String
                appCategory.title = sectionTitle
                let sectionType = sections[i]["type"] as! NSNumber
                appCategory.type = sectionType
                let sectionId = sections[i]["id"] as! NSNumber
                appCategory.id = sectionId
                // for Apps inside each section
                let lists = sections[i]["lists"] as! [String: AnyObject]
                 for j in 0..<lists.count{
                    let app = App()

                    let id = lists[j]["id"] as! NSNumber
                    app.id = id

                    let thumb = lists[j]["thumb"]! as! String

                    Alamofire.request(thumb).responseImage { response in

                        if let image = response.result.value {
                            app.image = image
                            print("image downloaded: \(image)")
                        }
                    }
                    let title = lists[j]["title"] as! String
                    app.title = title

                    apps.append(app)
                }
                appCategory.apps = apps
                appCategories.append(appCategory)
            }
            DispatchQueue.main.async(execute: {
                completionHandler(appCategories)
            })
        }catch{
            print("JSON Processing Failed")
        }
        }.resume()
}
}

我有这些类作为模型

class App: NSObject {
var id: NSNumber?
var title: String?
var image: UIImage?

}

我在 viewController 中调用featuredAppsviewDidLoad发生的是当视图加载标签出现但图像不在集合视图中时。

我需要滚动每一行以使图像出现。

我不知道问题在哪里???

4

1 回答 1

0

保留 url 并将 url 设置为图像视图使用 AlamofireImage。

imageView.af_setImage(withURL: url)
于 2017-02-08T10:14:00.967 回答