代码之家  ›  专栏  ›  技术社区  ›  ICL1901

使用两列大小相等的方形单元格显示UICollectionView

  •  0
  • ICL1901  · 技术社区  · 7 年前

    我正在努力完成以下工作,如有任何帮助将不胜感激:

    enter image description here

    this question ,但我得到的图像如下:

    enter image description here

    这是集合视图控制器的代码:

    class MainMenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    
    
        @IBOutlet var collectionView: UICollectionView!
        @IBOutlet weak var dateTimeLabel: MarqueeLabel!
        @IBOutlet weak var iconButton: UIButton!
        @IBOutlet var imageView: UIImageView!
    
        var iconButtonTimer: Timer!
        var upDateTimer: Timer!
    
        var statusString: String!
        var alertTitle: String!
        var networkStatusString: String!
    
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            collectionView.dataSource = self
            collectionView.delegate = self
    
            collectionView.register(UINib.init(nibName: "MainMenuCell", bundle: nil), forCellWithReuseIdentifier: "MainMenuCell")
    
            collectionView.collectionViewLayout = MainMenuLayout()
        }
    
    
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 8
        }
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 2
        }
    
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            print(#function)
            let padding: CGFloat =  50
            let collectionViewSize = collectionView.frame.size.width - padding
    
            return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
        }
    
    
    
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainMenuCell", for: indexPath)
    
             cell.backgroundColor = UIColor.red
    
            return cell
        }
    }
    

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  3
  •   Shehata Gamal    7 年前

    符合 UICollectionViewDelegateFlowLayout 并实施

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt section: Int) -> UIEdgeInsets {
    
        return UIEdgeInsetsMake(10, 10, 10, 10)
    }
    

    同时评论这一行

    collectionView.collectionViewLayout = MainMenuLayout()
    

    enter image description here

    推荐文章