0

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:

enter image description 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
    
    
    
    
}

}

koen
  • 5,383
  • 7
  • 50
  • 89
noorapps
  • 39
  • 1
  • 6
  • You are likely missing the connection between the outlets and the view but you likely need to start with the basic tutorials https://developer.apple.com/tutorials/app-dev-training#collection-views-and-navigation – lorem ipsum Dec 02 '22 at 13:53

0 Answers0