How to get indexpath.row insde insetForSectionAt to give different size for different indexpath.row

46 Views Asked by At
import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    var arrayInt = [0,1,2,3,4,5,6,7,8]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupCollectionView()
    }
    
    func setupCollectionView(){
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.registerCell(nib: Cell.nib, identifier: Cell.identifier)
    }
    
}

extension BillVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arrayInt.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:Cell.identifier, for: indexPath) as! Cell
    cell.lblPrice.text = “$49.5”     
   
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = self.collectionView.frame.width - 60
        return CGSize(width: (width / 2), height: (width / 2))
    }
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        self.viewDidLayoutSubviews()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 20, bottom: 10, right: 20)
    }
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
//        var edgeInsets = UIEdgeInsets()
//        arrayInt.forEach {
//            if $0 == arrayInt.count-1{
//                edgeInsets = UIEdgeInsets(top: 0, left: 80, bottom: 10, right: 80)
//            }
//            else{
//                edgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 10, right: 20)
//            }
////            print("foreach:",$0)
//        }
//
//        return edgeInsets
//    }
 
}

this is my code i want this to be applied that for arrayInt.count - 1 it get the different edgeInsets for indexpath of the section but i cant get the indexpath.row i have tried many ways but not achieving the desired result

i want this to be applied that for arrayInt.count - 1 it get the different edgeInsets for indexpath of the section but i cant get the indexpath.row i have tried many ways but not achieving the desired result

0

There are 0 best solutions below