使用swiftyJSON&重新加载UICollectionview TRON

时间:2017-09-01 17:27:57

标签: json swift xcode uicollectionview swifty-json

在使用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天开始编程,如果我的代码很傻就很抱歉。

1 个答案:

答案 0 :(得分:0)

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