我在使用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")




}


这是我的视图控制器,有一个模型页面

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/

10-12 01:48