6

I want to make two rows collection view in horizontal like below

enter image description here

I am new in iOS development, and some tutorials just give example how to make certain number of columns of collection view vertically, but i need to limit the number of rows of the collection view

I have tried to make the number of section to be two but still, the number of row still one

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource{

    @IBOutlet var collectionView: UICollectionView!


    let imageArray = [UIImage(named: "image1"),UIImage(named: "image2"),UIImage(named: "image3"),UIImage(named: "image4"),UIImage(named: "image5"),UIImage(named: "image6"),UIImage(named: "image8"),UIImage(named: "image9")]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCell
        cell.avatarView.image = imageArray[indexPath.item]
        return cell
    }


}

enter image description here

1
  • 1
    A collection view can be horizontal or vertical. Yours is horizontal. So it works exactly like the two-column vertical collection views you've read about. Just substitute "width" for "height" in the discussion of how to make the cells be the right size, and you're all set. It's trivial. No need for sections or anything else. Commented Sep 30, 2018 at 1:50

2 Answers 2

1

Please update the section inset

func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCell
    cell.avatarView.image = imageArray[indexPath.item]
    return cell
}
 public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let padding: CGFloat =  60
        let collectionViewSize = collectionView.frame.size.height - padding

        return CGSize(width: 100, height: collectionViewSize/2)
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Add below in your code

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: collectionView.frame.height/2, height: collectionView.frame.height/2)

    }

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.