Saturday, October 14, 2023
HomeiOS Developmentios - Easy methods to show solely 5 cells in CollectionView?

ios – Easy methods to show solely 5 cells in CollectionView?


i need to present solely 5 posts from my WordPress Web site into CollectionView in my Swift app. i’m very new to Swift. i’ve set this as url

https://www.sikhnama.com/wp-json/wp/v2/posts/?classes=4&per_page=5

and this get solely 5 posts from WordPress however in collectionView after 5 posts it repeats the posts, however i would like after 5 cells there shouldn’t be any extra cell and put up.
that is my code ..

 func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int{
    return newsData.depend + (newsData.depend/4)
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
     if (indexPath.merchandise % 4 == 3){
        
        let adcell = collectionView.dequeueReusableCell(withReuseIdentifier: "adcell", for: indexPath) as! RelatedViewCell
         
        
       
         adcell.banner.adUnitID = bannerAd
         adcell.banner.rootViewController = self
         adcell.banner.load(GADRequest())
         adcell.banner.delegate = self
        
       return adcell
        
    }
    
    else{
        
       
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postcell", for: indexPath) as! RelatedViewCell
        
        
        
        cell.setup(with: newsData[indexPath.row-(indexPath.row/4)])
        return cell
    }
}

i’ve tried this additionally

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int{
    return 5
}

then i get error on this line that “Index out of Vary”

 cell.setup(with: newsData[indexPath.row-(indexPath.row/4)])

additionally tried

cell.setup(with: newsData[indexPath.row])

however not works,, assist please



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments