如何使用 Alamofire 在 UICollectionView 单元格中使用其标签使图像出现在视图中?
How to make images appear in the view with its labels in UICollectionView cells using Alamofire?
我正在使用 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 中的 viewDidLoad
中调用 featuredApps
发生的事情是当视图加载标签时出现但图像不在集合视图中。
我需要滚动每一行才能显示图像。
我不知道问题在哪里???
保留 url 并将 url 设置为图像视图使用 AlamofireImage。
imageView.af_setImage(withURL: url)
我正在使用 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 中的 viewDidLoad
中调用 featuredApps
发生的事情是当视图加载标签时出现但图像不在集合视图中。
我需要滚动每一行才能显示图像。
我不知道问题在哪里???
保留 url 并将 url 设置为图像视图使用 AlamofireImage。
imageView.af_setImage(withURL: url)