i am very new to Xcode and trying to make an app from my WordPress Posts using Rest API JSON, i have fetched the data correctly but don't know how to display in cell using CollectionView. when i try it i am getting this error..
see Image Here:
this is the code i am using for collectionView.. i have a homeController with XIB file And HomeViewCell with XIB file
and this code is in homeController
func fetchPostData(completionHandler: @escaping ([Post]) -> Void ) {
let url = URL(string: "https://www.sikhnama.com/wp-json/wp/v2/posts/?per_page=30")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else {return}
do {
let postsData = try JSONDecoder().decode([Post].self, from: data)
completionHandler(postsData)
}
catch {
let error = error
print(String(describing: error))
}
}.resume()
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if (indexPath.row == self.newsData.count-1){
if(self.moreDataNum != 0){
self.view.isUserInteractionEnabled = false
self.fetchPostData { (posts) in
self.newsData = posts
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}
}
}
extension homeController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return self.newsData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! homeViewCell
cell.titlelbl.text = String(htmlEncodedString:self.newsData[indexPath.row].title.rendered)
return cell
}
}