如何获取indexpath.row insde insetForSectionAt为不同的indexpath.row提供不同的大小

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

提问人:DSR 提问时间:11/3/2023 更新时间:11/3/2023 访问量:26

问:

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
//    }
 
}

这是我的代码,我希望将其应用于 arrayInt.count - 1 它为该部分的 indexpath 获取不同的 edgeInsets,但我无法获得 indexpath.row 我尝试了很多方法,但没有达到预期的结果

我希望将其应用于 arrayInt.count - 1 它为该部分的 indexpath 获取不同的 edgeInsets,但我无法获得 indexpath.row 我尝试了很多方法,但没有达到预期的结果

数组 swift uicollectionview uicollectionviewcell uiedgeinsets

评论

0赞 Zeeshan Ahmad II 11/3/2023
这是一种完全糟糕的方法,因为您将插入到特定行。当您可以自由地通过给它们留边距(减小宽度)来设置单元格的大小时。

答: 暂无答案