【发布时间】:2018-07-10 11:57:55
【问题描述】:
我正在尝试使用服务器数据制作 collectionview 我在 数组并打印它以确保返回数据
extension ViewController : DataModelDelegate {
func didRecieveDataUpdate(data: Data) {
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
// parse json array
guard let jsonArray = responseJSON as? [[String: Any]]
else {
return
}
for dic in jsonArray {
self.listOfCategories.append(MCategories(dic))
}
print(self.listOfCategories[1].Name)
print(self.listOfCategories.count)
}
}
但是这个数组在 collectionview() 中返回 0 > int func
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(self.listOfCategories.count)
return listOfCategories.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellcategory:CVC_Categories = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CVC_Categories
cellcategory.setCategoryData(categories: listOfCategories[indexPath.row])
return cellcategory
}
我在 viewdidload() 中调用这个代表
var listOfCategories = [MCategories]()
override func viewDidLoad() {
super.viewDidLoad()
gettoken()
self.dataModel.delegate = self
dataModel.requestData()
cv_categories.delegate = self
cv_categories.dataSource = self
}
【问题讨论】:
-
你必须在
didRecieveDataUpdate末尾的集合视图上调用reloadData() -
也尝试在主队列 DispatchQueue.main.async {} 中重新加载集合视图
标签: ios json swift uicollectionview delegates