-1

I would like to have UICollectionView with Horizontal Scrolling and Layout each section on it's own row from left to right.

Currently my CollectionView with Horizontal Scrolling has a vertical layout for each section.

current layout

Ideally I would like the following layout with horizontal scrolling:

enter image description here

So my question is how can I have horizontal scrolling on my UICollectionView where each horizontal row from left to right is laid out in one row. (see second diagram) Each section would occupy a different row.

Also each row should scroll horizontally at the same time (like a spreadsheet).

Ranknoodle
  • 847
  • 12
  • 28
  • What have you tried so far? Can you provide us with any code? – gmdev Jan 28 '20 at 02:54
  • I have tried a solution whereby each row of a UITableViewCell in a UITableVIew contains a UICollectionView with horizontal scrolling however I want all rows to scroll horizontally at the same time not individually so that solution will not work for my purposes. – Ranknoodle Jan 28 '20 at 15:34

1 Answers1

2

You have to combine Collection-view and Tableview for having horizontal scrolling on UICollectionView where each horizontal row from left to right is laid out in one row. Each section would occupy a different row.

      class DesignCollectionViewCell: UICollectionViewCell {

        @IBOutlet weak var lbltext: UILabel!
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    }

    class DesignTableViewCell: UITableViewCell {

        @IBOutlet weak var designCollectionView: UICollectionView!
        var numberofScection = Int()

        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
            self.designCollectionView.register(UINib(nibName: "DesignCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "DesignCollectionViewCell")
            self.designCollectionView.dataSource = self
            self.designCollectionView.delegate = self
            let layout = UICollectionViewFlowLayout()
            layout.scrollDirection = .horizontal
            self.designCollectionView.collectionViewLayout = layout

        }

        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)

            // Configure the view for the selected state
        }

    }

    extension DesignTableViewCell: UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{



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

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DesignCollectionViewCell", for: indexPath) as! DesignCollectionViewCell
            cell.lbltext.text = "section \(self.numberofScection) indexpath \(indexPath.row)"
            return cell
        }

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: 170, height: 100.0)
        }
    }

class ViewController: UIViewController {

    @IBOutlet weak var designTableViewCell: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.designTableViewCell.register(UINib(nibName: "DesignTableViewCell", bundle: nil), forCellReuseIdentifier: "DesignTableViewCell")
        self.designTableViewCell.dataSource = self
        self.designTableViewCell.delegate = self

    }


}


extension ViewController: UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DesignTableViewCell") as! DesignTableViewCell
        cell.designCollectionView.reloadData()
        cell.numberofScection = indexPath.row
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 110
    }

}
Jay Patel
  • 21
  • 2
  • I actually created this exact solution however I wanted ALL horizontal scrolling to happen for all rows at the same time not individual rows. I apologize as I did not have that in the question. – Ranknoodle Jan 28 '20 at 15:32