gpt4 book ai didi

json - 使用 swiftyJSON 和 TRON 重新加载 UICollectionview

转载 作者:行者123 更新时间:2023-11-30 12:11:25 27 4
gpt4 key购买 nike

在使用 TRON pod 获取 json 数据后,我无法重新加载 UICollectionview(它就像 alamofire,但我认为结构不同)我使用 swiftyJSON 解析了它我正在寻找大约三天的答案,但我不知道我错过了什么...

    import UIKit
import SwiftyJSON
import TRON

class FeaturedAppViewController: UICollectionViewController ,
UICollectionViewDelegateFlowLayout {

private let cellID = "cellId"


var appCategories : [AppCategory]?

override func viewDidLoad() {
super.viewDidLoad()



fetchHomeApps()

collectionView?.backgroundColor = .white

collectionView?.register(CategoryCell.self, forCellWithReuseIdentifier: cellID)

}


class Home : JSONDecodable{

var apps : [App]

required init(json: JSON) throws {
print("now ready to parse :\n", json)

var apps = [App]()

let catJSON = json["categories"]
let array = json["categories"].array
for app in array!{
for index in 0...catJSON.count - 1 {
let name = app["apps"][index]["Name"].stringValue
let id = app["apps"][index]["Id"].intValue
let imageName = app["apps"][index]["ImageName"].stringValue
let category = app["apps"][index]["Category"].stringValue
let price = app["apps"][index]["Price"].doubleValue


let appsIdentification = App(iD: id, name: name, category: category, price: price, imageName: imageName)
apps.append(appsIdentification)
}
}
self.apps = apps

}
}

class JSONError : JSONDecodable {

required init(json: JSON) throws {
print("josn Error")
}

}
fileprivate func fetchHomeApps() {
print("123")

let request : APIRequest<AppCategory , JSONError> = tron.request("/appstore/featured")

request.perform(withSuccess: { (AppCategory) in

print("Successfully Fetched")
print(AppCategory.apps.count)
self.collectionview.reloaddata()

}) { (err) in

print("couldnt Fetch babe \n" , err)

}
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

if let count = appCategories?.count {
return count
}else{
return 0
}

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! CategoryCell

cell.appCategory = appCategories?[indexPath.item]

return cell

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

return CGSize(width: view.frame.width , height: 230)

}

let tron = TRON(baseURL: "http://www.statsallday.com")




}

这是我的 View Controller ,有一个模型页面

import UIKit
import SwiftyJSON
import TRON


class AppCategory : NSObject , JSONDecodable {

var name : String?
var type : String?
let Url = "http://www.statsallday.com/appstore/featured"

var apps : [App]

required init(json: JSON) throws {
print("now ready to parse :\n", json)

var apps = [App]()

let catJSON = json["categories"]
let array = json["categories"].array
for app in array!{
for index in 0...catJSON.count - 1 {
let name = app["apps"][index]["Name"].stringValue
let id = app["apps"][index]["Id"].intValue
let imageName = app["apps"][index]["ImageName"].stringValue
let category = app["apps"][index]["Category"].stringValue
let price = app["apps"][index]["Price"].doubleValue


let appsIdentification = App(iD: id, name: name, category: category, price: price, imageName: imageName)
apps.append(appsIdentification)
}
}
self.apps = apps

}
}


struct App {

let iD : Int
let name : String
let category : String
let price : Double
let imageName : String

}

我想我应该在 fetchHomeApps 函数中添加一些东西,但我不知道什么......

实际上,我从 34 天起就开始编程,如果我的代码很愚蠢,我很抱歉。

最佳答案

当您从API获取数据并使用swiftyJson解析时,您应该调用self.yourCollectionView.reloadData()。仅当 appCategories 具有数据而不是空数组对象时。

关于json - 使用 swiftyJSON 和 TRON 重新加载 UICollectionview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46005314/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com