2017-08-20 18 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

相关问题