0

我无法将数据从 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!
        }

4

2 回答 2

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()
}
于 2018-12-24T21:03:48.373 回答
0

在服务中获得结果后,委托给您的控制器并重新加载 collectionView。

于 2018-12-25T06:36:39.763 回答