我无法将数据从api加载到CollectionView。从api接收数据后如何加载数据 [在此处输入图片描述] [1]
func service() {
Alamofire.request("https://api.letsbuildthatapp.com/appstore/featured").responseJSON { response in
print("Request: \(String(describing: response.request))") // original url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // response serialization result
if let json = response.result.value {
let json = JSON(json)
for i in 0..<json["bannerCategory"]["apps"].count{
self.arrTitle.append(json["bannerCategory"]["apps"][i]["ImageName"].string!)
}
if self.arrTitle.count > 0 {
self.collectionView.reloadData()
}
}
}
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
var reusable: UICollectionReusableView? = nil
if (kind == UICollectionView.elementKindSectionHeader) {
let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerId, for: indexPath) as! CollectionReusableView
view.backgroundColor = .white
view.label.text = self.arrTitle[indexPath.row]
reusable = view
}
return reusable!
}
答案 0 :(得分:0)
检查集合视图数据源方法的实现,并将reloadData()
放入主线程。
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.arr[section].count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return self.arr.count
}
DispatchQueue.main.async {
self.collectionView.reloadData()
}
答案 1 :(得分:0)
将结果投入使用后,委托给您的控制器并重新加载collectionView。