2017-07-17 3 views
0

UICollectionviewCellでJSONを解析したいと思います。私は2つのUICollectionviewCellを持つcollectionViewControllerを持っています。 collectionViewControllerの最初のセルは背景のスクロールに、2番目のセルはJSONの解析を行います。コードにエラーはありません。これは私のJSONコードです。UICollectionViewCellのjsonを素早く解析する

var oCategoryFilter: CategoryFilter? { 
    didSet { 

     if let name = oCategoryFilter?.totalItem { 
      totalItemLabel.text = name 
     } 

     appsCollectionView.reloadData() 
    } 
} 

var arrProduct: [Product]? 

func getPropductListByCategory(){ 

    let category_id:String; 

    category_id = "21" 

    let url = URL(string: UtilityController.BASE_URL+"/products/"+category_id) 

    URLSession.shared.dataTask(with:url!) { (urlContent, response, error) in 
     if error != nil { 
      print(error) 

     } 
     else { 
      do { 
       let json = try JSONSerialization.jsonObject(with: urlContent!) as! [String:Any] 

       print(json) 

       let items = json["categories"] as? [[String: Any]] ?? [] 

       items.forEach { item in 

        let oProduct = Product() 
        //oProduct.id = item["id"] as? String 
        oProduct.image = item["image"] as? String 
        oProduct.name = item["name"] as? String 
        oProduct.ar_name = item["ar_name"] as? String 
        //oProduct.description = item["description"] as? String 
        oProduct.ar_description = item["ar_description"] as? String 
        oProduct.price = item["price"] as? String 
        oProduct.quantity = item["quantity"] as? String 
        oProduct.is_featured = item["is_featured"] as? String 
        oProduct.seller_id = item["seller_id"] as? String 
        oProduct.payment_required = item["payment_required"] as? String 
        oProduct.is_editors_choice = item["is_editors_choice"] as? String 
        oProduct.created_at = item["created_at"] as? String 
        oProduct.updated_at = item["updated_at"] as? String 

        self.arrProduct?.append(oProduct) 
       } 
       print(url) 
      } catch let error as NSError { 
       print(error) 
      } 
     } 

     DispatchQueue.main.async(execute: { 
      self.appsCollectionView.reloadData() 
     }) 


     }.resume() 
} 

答えて

1

あなたの機能はいつ呼びますか?すべてのセルがロードされているときにCollectionViewのメソッドを呼び出す必要がありますが、それは実際には悪いことです。コレクションビューをスクロールまたは再読み込みするたびに再び解析されるためです。

特殊なクラスで解析する必要があります。コレクションビューで呼び出すと、最後に解析オブジェクトがセルに送信されます。

関連する問題