2017-08-20 17 views
0

我正在使用Alamofire,但在這種情況下,當我調試Alamofire代碼不會執行,現在我嘗試手動創建url並設置標題,然後調用我的API URL。爲什麼在我的Collection View Cell中得到零?

我的集合視圖類如下。

// 
// CategoryViewController.swift 
// iRate 
// 
// Created by Ammar Khan on 16/08/2017. 
// Copyright © 2017 theistudio. All rights reserved. 
// 

import UIKit 
import Alamofire 
import SwiftyJSON 
import SDWebImage 

class CategoryViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate 
{ 
    @IBOutlet var myCollectionView: UICollectionView! 
    var catArr:[String] = [] 
    var catImage:[String] = [] 

    let categoriesUrl = "http://127.0.0.1:8000/api/places/categories" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    override func viewDidAppear(_ animated: Bool) { 

     loadCatList() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 2 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for:indexPath) as! CategoryCell 
     print("Reached over here \(self.catArr.count)") 
     cell.categoryName.text = self.catArr[indexPath.row] 
     return cell 
    } 

    func loadCatList(){ 
     let networkSession = URLSession.shared 

     var request = URLRequest(url: URL(string: self.categoriesUrl)!) 

     request.setValue("application/json", forHTTPHeaderField: "Content-Type") 

     let dataTask = networkSession.dataTask(with: request) { (data, response, error) in 
      print("Data downloaded") 

      let jsonReadable = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 

      print(jsonReadable!) 
      do{ 

       let json = JSON(jsonReadable!) 

       for (_,json):(String, JSON) in json { 
        let catName = json["category_name"].stringValue 
        let catImage = json["image"].stringValue 

        self.catArr.append(catName) 
        self.catImage.append(catImage) 
       } 
      } 
      catch{ 
       print("we have a JSON exception") 
      } 
     } 
     // 
     dataTask.resume() 
     print("Downloading data") 
    } 

    func getCount(){ 
     print(self.catArr.count) 
    } 
} 

我試過各種方法向我的字典添加數據,但我失敗了,它給了我零。我不確定我在這裏做錯了什麼。

PS:這不是我想要檢索的,只是爲了測試目的,因此只接收兩個值並將它們添加到字典中。

最終結果:

的答案建議下面是其中的問題,之後我收到了數據,並將它們附加到我的陣列的方式是一些如何錯了,所以更換我裏面做下面的代碼聲明使其工作。

let json = JSON(data!) 

print("The json data is \(json)") 

for (_,json):(String, JSON) in json { 

    let catName = json["category_name"].stringValue 
    print("The catImage is \(catName)") 
    let catImage = json["image"].stringValue 
    self.catArr.append(catName) 
    self.catImage.append(catImage) 
} 

DispatchQueue.main.async { 
    self.myCollectionView.reloadData() 
} 

回答

1

您收到爲零,因爲網絡電話是異步,所以會顯示您的表時,它是零的那一刻,當數據來自你不重裝的CollectionView,所以你需要添加後,以下JSON的-in環

DispatchQueue.main.async { 
    self.myCollectionView.reloadData() 
} 

更新:

您應該返回的項目,如在你的數據源,在這種情況下是self.catArr元素的數量的數量,所以修改代碼如下:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return self.catArr.count 
} 
+0

我在for循環之後在我的do語句中粘貼了這個。問題仍未解決。一旦應用程序啓動,它崩潰並給我一個超出界限的索引。這是第一次加載cellForItemAt函數,它打破了那裏,因爲數據尚未加載 – indexOutOfBounds

+0

@indexOutOfBounds請檢查我的更新答案,並給出您的反饋 – 3stud1ant3

+0

爲了測試的目的,我有部分被返回2但它解決了錯誤。謝謝你,但問題保持不變。因此,appforItemAt函數不執行,因此應用程序啓動時不顯示任何內容。 Count仍然保持爲0. – indexOutOfBounds

相關問題