Ios 无法获取要在集合视图中显示的数组(Swift 4)
我测试了集合视图可以显示的内容。但是,我无法在集合视图中检索和添加数组结果 这是我的密码:Ios 无法获取要在集合视图中显示的数组(Swift 4),ios,arrays,swift4,collectionview,Ios,Arrays,Swift4,Collectionview,我测试了集合视图可以显示的内容。但是,我无法在集合视图中检索和添加数组结果 这是我的密码: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell",
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MainPageCollectionViewCell
cell.FoodTitle.text = arr[indexPath.row].title
cell.Food.image = arr[indexPath.row].image_url
return cell
}
关于数组,它是fetchFoodList中的一个函数
func fetchFoodList(){
let url = URL(string: "MYAPI.json")
guard let unwrappedUrl = url else { return }
let task = URLSession.shared.dataTask(with: unwrappedUrl, completionHandler: {(data, response, error)in
if error != nil{
print(error!)
} else {
if let urlContent = data{
do {
let json = try JSON(data:data!)
let recipes = json["recipes"]
for arr in recipes.arrayValue{
print(arr["title"])
print(arr["image_url"])
}
}
catch{
print("JSON Processing Failed")
}
}
}
})
task.resume()
}
}
但是,可以在控制台中显示数组结果标题和图像url 追加数组列表后,需要像这样重新加载集合
collectionView.reloadData()您需要重新加载collectionView。在重新加载collection view之前,您必须创建模型的实例并将其添加到
arr
。我可以知道您创建模型实例的意思吗?我在func fetchFoodList()中有arr对于collectionView和tableView,需要在获取或创建数组列表后重新加载数据