Ios 为什么我的收藏视图单元格中没有?
我正在使用Alamofire,但在这种情况下,当我调试Alamofire代码时,它不会执行,现在我尝试手动创建url并设置标题,然后调用API url 我的集合视图类如下所示Ios 为什么我的收藏视图单元格中没有?,ios,api,swift3,uicollectionviewcell,Ios,Api,Swift3,Uicollectionviewcell,我正在使用Alamofire,但在这种情况下,当我调试Alamofire代码时,它不会执行,现在我尝试手动创建url并设置标题,然后调用API url 我的集合视图类如下所示 // // CategoryViewController.swift // iRate // // Created by Ammar Khan on 16/08/2017. // Copyright © 2017 theistudio. All rights reserved. // import UIKit i
//
// 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:这不是我想要检索的,只是为了测试目的,因此只接收两个值并将它们添加到dict
最终结果:
正如下面给出的答案所示,这是问题之一,在这之后,我接收数据并将其附加到数组中的方式有点错误,因此在do语句中替换以下代码使其工作
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()
}
您收到的是nil,因为网络调用是异步的,所以当您的表显示时,此时它是nil,并且当数据到来时,您不会重新加载collectionview,因此您需要在JSON的for in循环之后添加以下内容
DispatchQueue.main.async {
self.myCollectionView.reloadData()
}
更新:
您应该将项目数作为数据源中的元素数返回,在本例中为self.catar,因此请按如下方式修改代码:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.catArr.count
}
我已经在for循环之后的do语句中粘贴了这个。这个问题还没有解决。应用程序一启动就会崩溃,并给我一个超出范围的索引。它首先加载cellForItemAt函数,由于数据尚未加载,它在那里中断yet@indexOutOfBounds请检查我更新后的答案,并给出反馈,以便于测试。我已经返回了部分2,但它解决了错误。谢谢你,但问题还是一样。因此,当应用程序启动时,cellforItemAt函数不会执行,不会显示任何内容。计数仍然保持为0。@IndexOutfBounds它不显示数据,因为在应用程序开始时,arr中没有元素,您需要在开始时填充数组,因为loadCatList方法是异步网络调用,所以加载数据需要一些时间,希望它能帮助我在下载loadCatList()函数中的数据时填充数组?我是否应该调用viewDidLoad()中的函数?我必须在下载数据后填充它们,这就是我正在做的。我已经对它进行了调试,即使在调用下载的数据之后,它也会将count返回为0。您可以在控制台中看到打印的数据,然后将其计为0