1

I am trying to emulate a hopscotch board pattern in my collectionView that will require a repeating 1-2-1 configuration (Example below), but cannot see any way to implement it. The pattern is required in 3 different sections of my collectionView. The collectionViewCell requires a 1:1 aspect ratio. The collectionView currently displays in a two column format for the cells.

enter image description here

Code currently as follows:

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int   {
     return 3
 }


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    if section == 0 {
        return 7
    } else if section == 1 {
        return 6
    } else {
        return 8
    }
    
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    
    return CGSize(width: collectionView.bounds.size.width, height: 20)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 10
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 10
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let totalWidth = (collectionView.frame.width - 20)
    
    let numberOfCellsPerRow = 2
    let dimensions = CGFloat(Int(totalWidth) / numberOfCellsPerRow)
    
    return CGSize(width: dimensions , height: dimensions)
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! BaseCollectionViewCell
    
    
    if indexPath.section == 0 {
        if indexPath.row == 0 {
            
            cell.titleLabel.text = "cell 1"
            return cell
            }
    } etc...
     

    override func viewDidLoad() {
    super.viewDidLoad()

        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.collectionViewLayout = UICollectionViewFlowLayout()
    
    
    }
zeytin
  • 5,545
  • 4
  • 14
  • 38
Ryan Hampton
  • 319
  • 1
  • 4
  • 21
  • You can't do that with the basic flow, you need to create your own with subclassing the Flow Layout. – Larme Nov 19 '20 at 12:03
  • Thanks for the response. Do you have any suggestions on how to start this process? I'm not very familiar with flowlayout – Ryan Hampton Nov 19 '20 at 12:35
  • @RyanHampton - as a general rule, collection views are used to allow automatic scrolling when there are too many cells to fit in the view frame. You may find it much simpler to use something else ... such as stack views. – DonMag Nov 19 '20 at 20:29

0 Answers0