UICollectionView to read from left to right with multiple rows










0















I am creating a UICollectionView that has paging enabled and scrolls from left to right. I am using an NSFetchedResultsController to retrieve results from CoreData, which means it uses collection view sections rather than rows. The collection view has 2 rows and therefore appears like the following screenshot (where the order goes top row, bottom row, top row, bottom row etc):



enter image description here



However, I need the collection view to read from left to right like the following 2 screen shots:



enter image description hereenter image description here



Could you please advise on how I would do this in Swift?










share|improve this question
























  • so b has IndexPath(row: 1, section: 0) ?

    – marosoaie
    Nov 15 '18 at 16:34












  • @marosoaie I've edited my question with more information. But basically I'm using an NSFetchedResultsController which uses collection view sections.

    – Adam Altinkaya
    Nov 15 '18 at 16:52











  • then maybe you need to sort your data so that it matches the order you want to achieve?

    – marosoaie
    Nov 15 '18 at 16:54











  • iirc the flow layout makes the cells go from left to right, and when there's no more room the next row starts, and so on, so you can only get it the way you want by setting a sort descriptor on your fetch request

    – marosoaie
    Nov 15 '18 at 16:56











  • Can you use instead of a,b,c,d,e,f,g etc... rather IndexPath values how you would like to have it ? Also is the last screenshot the preview of page 2 ?

    – Grzegorz Krukowski
    Nov 21 '18 at 14:29
















0















I am creating a UICollectionView that has paging enabled and scrolls from left to right. I am using an NSFetchedResultsController to retrieve results from CoreData, which means it uses collection view sections rather than rows. The collection view has 2 rows and therefore appears like the following screenshot (where the order goes top row, bottom row, top row, bottom row etc):



enter image description here



However, I need the collection view to read from left to right like the following 2 screen shots:



enter image description hereenter image description here



Could you please advise on how I would do this in Swift?










share|improve this question
























  • so b has IndexPath(row: 1, section: 0) ?

    – marosoaie
    Nov 15 '18 at 16:34












  • @marosoaie I've edited my question with more information. But basically I'm using an NSFetchedResultsController which uses collection view sections.

    – Adam Altinkaya
    Nov 15 '18 at 16:52











  • then maybe you need to sort your data so that it matches the order you want to achieve?

    – marosoaie
    Nov 15 '18 at 16:54











  • iirc the flow layout makes the cells go from left to right, and when there's no more room the next row starts, and so on, so you can only get it the way you want by setting a sort descriptor on your fetch request

    – marosoaie
    Nov 15 '18 at 16:56











  • Can you use instead of a,b,c,d,e,f,g etc... rather IndexPath values how you would like to have it ? Also is the last screenshot the preview of page 2 ?

    – Grzegorz Krukowski
    Nov 21 '18 at 14:29














0












0








0


1






I am creating a UICollectionView that has paging enabled and scrolls from left to right. I am using an NSFetchedResultsController to retrieve results from CoreData, which means it uses collection view sections rather than rows. The collection view has 2 rows and therefore appears like the following screenshot (where the order goes top row, bottom row, top row, bottom row etc):



enter image description here



However, I need the collection view to read from left to right like the following 2 screen shots:



enter image description hereenter image description here



Could you please advise on how I would do this in Swift?










share|improve this question
















I am creating a UICollectionView that has paging enabled and scrolls from left to right. I am using an NSFetchedResultsController to retrieve results from CoreData, which means it uses collection view sections rather than rows. The collection view has 2 rows and therefore appears like the following screenshot (where the order goes top row, bottom row, top row, bottom row etc):



enter image description here



However, I need the collection view to read from left to right like the following 2 screen shots:



enter image description hereenter image description here



Could you please advise on how I would do this in Swift?







ios swift uicollectionview uicollectionviewlayout






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:51







Adam Altinkaya

















asked Nov 15 '18 at 16:12









Adam AltinkayaAdam Altinkaya

2471920




2471920












  • so b has IndexPath(row: 1, section: 0) ?

    – marosoaie
    Nov 15 '18 at 16:34












  • @marosoaie I've edited my question with more information. But basically I'm using an NSFetchedResultsController which uses collection view sections.

    – Adam Altinkaya
    Nov 15 '18 at 16:52











  • then maybe you need to sort your data so that it matches the order you want to achieve?

    – marosoaie
    Nov 15 '18 at 16:54











  • iirc the flow layout makes the cells go from left to right, and when there's no more room the next row starts, and so on, so you can only get it the way you want by setting a sort descriptor on your fetch request

    – marosoaie
    Nov 15 '18 at 16:56











  • Can you use instead of a,b,c,d,e,f,g etc... rather IndexPath values how you would like to have it ? Also is the last screenshot the preview of page 2 ?

    – Grzegorz Krukowski
    Nov 21 '18 at 14:29


















  • so b has IndexPath(row: 1, section: 0) ?

    – marosoaie
    Nov 15 '18 at 16:34












  • @marosoaie I've edited my question with more information. But basically I'm using an NSFetchedResultsController which uses collection view sections.

    – Adam Altinkaya
    Nov 15 '18 at 16:52











  • then maybe you need to sort your data so that it matches the order you want to achieve?

    – marosoaie
    Nov 15 '18 at 16:54











  • iirc the flow layout makes the cells go from left to right, and when there's no more room the next row starts, and so on, so you can only get it the way you want by setting a sort descriptor on your fetch request

    – marosoaie
    Nov 15 '18 at 16:56











  • Can you use instead of a,b,c,d,e,f,g etc... rather IndexPath values how you would like to have it ? Also is the last screenshot the preview of page 2 ?

    – Grzegorz Krukowski
    Nov 21 '18 at 14:29

















so b has IndexPath(row: 1, section: 0) ?

– marosoaie
Nov 15 '18 at 16:34






so b has IndexPath(row: 1, section: 0) ?

– marosoaie
Nov 15 '18 at 16:34














@marosoaie I've edited my question with more information. But basically I'm using an NSFetchedResultsController which uses collection view sections.

– Adam Altinkaya
Nov 15 '18 at 16:52





@marosoaie I've edited my question with more information. But basically I'm using an NSFetchedResultsController which uses collection view sections.

– Adam Altinkaya
Nov 15 '18 at 16:52













then maybe you need to sort your data so that it matches the order you want to achieve?

– marosoaie
Nov 15 '18 at 16:54





then maybe you need to sort your data so that it matches the order you want to achieve?

– marosoaie
Nov 15 '18 at 16:54













iirc the flow layout makes the cells go from left to right, and when there's no more room the next row starts, and so on, so you can only get it the way you want by setting a sort descriptor on your fetch request

– marosoaie
Nov 15 '18 at 16:56





iirc the flow layout makes the cells go from left to right, and when there's no more room the next row starts, and so on, so you can only get it the way you want by setting a sort descriptor on your fetch request

– marosoaie
Nov 15 '18 at 16:56













Can you use instead of a,b,c,d,e,f,g etc... rather IndexPath values how you would like to have it ? Also is the last screenshot the preview of page 2 ?

– Grzegorz Krukowski
Nov 21 '18 at 14:29






Can you use instead of a,b,c,d,e,f,g etc... rather IndexPath values how you would like to have it ? Also is the last screenshot the preview of page 2 ?

– Grzegorz Krukowski
Nov 21 '18 at 14:29













3 Answers
3






active

oldest

votes


















0














First, I think it could help if you provide an example how your cells are sectioned currently (e.g. section 0: a, b, c, d, section 1: e, f, ...) and how you want the different sections to be displayed/layouted.




I'd suggest to create a custom UICollectionViewLayout subclass.



In the prepareLayout method you should calculate and cache the UICollectionViewLayoutAttributes for each cell.



Create variables:



let xOffset = 0.0 // leftContentInset within collectionView.bounds
let yOffsetRow0 = 0.0 // y offset of first row within collectionView.bounds
let yOffsetRow1 = 50.0 // y offset of second row within collectionView.bounds
var currentXOffset : CGFloat = xOffset
var currentPage : Int = 0


  1. calculate the frames until first row is filled to right screen edge (CGRect(currentXOffset, yOffsetRow0, cellWidth, cellHeight), increase currentXOffset)

  2. reset currentXOffset = xOffset

  3. repeat 1. with yOffsetRow1 for second row

  4. increment currentPage += 1

  5. reset currentXOffset = xOffset + (collectionView.frame.width * currentPage) // for next page

  6. repeat steps 1-5 until layout for all cells is calculated





share|improve this answer






























    0














    As you said, you need
    1. Paging enabled
    2. Scrolling from left to right
    3. Multiple sections.



    It's a little bit confusing what exactly you want to design. It will be much better if can share proper UI screenshot.



    As I understand your problem.



    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)


    Check have you assign the horizontal layout to your collectionView.






    share|improve this answer






























      0














      What I would do is this:



      1) Sort your data the same way you do it now so that your collection view would look like this:



      ---------
      |0|2|4|6|
      ---------
      |1|3|5|7|
      ---------


      2) In your cellForIndexPath method of your collection view data source return the correct element so that your collection view turns into this:



      ---------
      |0|1|2|3|
      ---------
      |4|5|6|7|
      ---------


      To do that you can use this logic:



      public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
      var elementIndex: Int!
      if indexPath.row % 2 == 0
      // 0 / 2 = 0
      // 2 / 2 = 1
      // 4 / 2 = 2, etc
      elementIndex = indexPath.row / 2

      else
      // (1 / 2) + round(8.0 / 2.0) = 4
      // (3 / 2) + round(8.0 / 2.0) = 5
      // (5 / 2) + round(8.0 / 2.0) = 6, etc
      elementIndex = (indexPath.row / 2) + Int(round(Double(elements.count) / 2.0))


      // Dequeue your cell
      // let cell = ...

      cell.element = elements[elementIndex]
      return cell



      Basically this should return your elements in the correct order.






      share|improve this answer























      • Hi, thanks for your help. Your 2nd solution half works, the only problem is that I have multiple pages in the collection view, and making the changes above, the collection view now reads like 0-1-2-3 4-5-6-7 8-9-10-11 on the top row, and 12-13-14-15 16-17-18-19 20-21-22-23 on the bottom row

        – Adam Altinkaya
        Dec 3 '18 at 11:22











      • Well isn't that what it should read?

        – Mihai Fratu
        Dec 3 '18 at 15:49











      • Oh! I just realized: you want to go 0-1-2-3 on the 1st row of 1st page, then 4-5-6-7 on the 2nd row of the 1st page, and then do the same for the 2nd page: 8-9-10-11 on the 1st row and 12-13-14-15 on the 2nd row of the 2nd page... Is this right? If so, yes, my solution does not do that :(

        – Mihai Fratu
        Dec 3 '18 at 15:51











      • Yes that is the way I need it read! Ah no worries, thanks for your help!

        – Adam Altinkaya
        Dec 3 '18 at 16:13










      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323564%2fuicollectionview-to-read-from-left-to-right-with-multiple-rows%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      First, I think it could help if you provide an example how your cells are sectioned currently (e.g. section 0: a, b, c, d, section 1: e, f, ...) and how you want the different sections to be displayed/layouted.




      I'd suggest to create a custom UICollectionViewLayout subclass.



      In the prepareLayout method you should calculate and cache the UICollectionViewLayoutAttributes for each cell.



      Create variables:



      let xOffset = 0.0 // leftContentInset within collectionView.bounds
      let yOffsetRow0 = 0.0 // y offset of first row within collectionView.bounds
      let yOffsetRow1 = 50.0 // y offset of second row within collectionView.bounds
      var currentXOffset : CGFloat = xOffset
      var currentPage : Int = 0


      1. calculate the frames until first row is filled to right screen edge (CGRect(currentXOffset, yOffsetRow0, cellWidth, cellHeight), increase currentXOffset)

      2. reset currentXOffset = xOffset

      3. repeat 1. with yOffsetRow1 for second row

      4. increment currentPage += 1

      5. reset currentXOffset = xOffset + (collectionView.frame.width * currentPage) // for next page

      6. repeat steps 1-5 until layout for all cells is calculated





      share|improve this answer



























        0














        First, I think it could help if you provide an example how your cells are sectioned currently (e.g. section 0: a, b, c, d, section 1: e, f, ...) and how you want the different sections to be displayed/layouted.




        I'd suggest to create a custom UICollectionViewLayout subclass.



        In the prepareLayout method you should calculate and cache the UICollectionViewLayoutAttributes for each cell.



        Create variables:



        let xOffset = 0.0 // leftContentInset within collectionView.bounds
        let yOffsetRow0 = 0.0 // y offset of first row within collectionView.bounds
        let yOffsetRow1 = 50.0 // y offset of second row within collectionView.bounds
        var currentXOffset : CGFloat = xOffset
        var currentPage : Int = 0


        1. calculate the frames until first row is filled to right screen edge (CGRect(currentXOffset, yOffsetRow0, cellWidth, cellHeight), increase currentXOffset)

        2. reset currentXOffset = xOffset

        3. repeat 1. with yOffsetRow1 for second row

        4. increment currentPage += 1

        5. reset currentXOffset = xOffset + (collectionView.frame.width * currentPage) // for next page

        6. repeat steps 1-5 until layout for all cells is calculated





        share|improve this answer

























          0












          0








          0







          First, I think it could help if you provide an example how your cells are sectioned currently (e.g. section 0: a, b, c, d, section 1: e, f, ...) and how you want the different sections to be displayed/layouted.




          I'd suggest to create a custom UICollectionViewLayout subclass.



          In the prepareLayout method you should calculate and cache the UICollectionViewLayoutAttributes for each cell.



          Create variables:



          let xOffset = 0.0 // leftContentInset within collectionView.bounds
          let yOffsetRow0 = 0.0 // y offset of first row within collectionView.bounds
          let yOffsetRow1 = 50.0 // y offset of second row within collectionView.bounds
          var currentXOffset : CGFloat = xOffset
          var currentPage : Int = 0


          1. calculate the frames until first row is filled to right screen edge (CGRect(currentXOffset, yOffsetRow0, cellWidth, cellHeight), increase currentXOffset)

          2. reset currentXOffset = xOffset

          3. repeat 1. with yOffsetRow1 for second row

          4. increment currentPage += 1

          5. reset currentXOffset = xOffset + (collectionView.frame.width * currentPage) // for next page

          6. repeat steps 1-5 until layout for all cells is calculated





          share|improve this answer













          First, I think it could help if you provide an example how your cells are sectioned currently (e.g. section 0: a, b, c, d, section 1: e, f, ...) and how you want the different sections to be displayed/layouted.




          I'd suggest to create a custom UICollectionViewLayout subclass.



          In the prepareLayout method you should calculate and cache the UICollectionViewLayoutAttributes for each cell.



          Create variables:



          let xOffset = 0.0 // leftContentInset within collectionView.bounds
          let yOffsetRow0 = 0.0 // y offset of first row within collectionView.bounds
          let yOffsetRow1 = 50.0 // y offset of second row within collectionView.bounds
          var currentXOffset : CGFloat = xOffset
          var currentPage : Int = 0


          1. calculate the frames until first row is filled to right screen edge (CGRect(currentXOffset, yOffsetRow0, cellWidth, cellHeight), increase currentXOffset)

          2. reset currentXOffset = xOffset

          3. repeat 1. with yOffsetRow1 for second row

          4. increment currentPage += 1

          5. reset currentXOffset = xOffset + (collectionView.frame.width * currentPage) // for next page

          6. repeat steps 1-5 until layout for all cells is calculated






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 12:16









          Robin SchmidtRobin Schmidt

          1515




          1515























              0














              As you said, you need
              1. Paging enabled
              2. Scrolling from left to right
              3. Multiple sections.



              It's a little bit confusing what exactly you want to design. It will be much better if can share proper UI screenshot.



              As I understand your problem.



              let layout = UICollectionViewFlowLayout()
              layout.scrollDirection = .horizontal
              let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)


              Check have you assign the horizontal layout to your collectionView.






              share|improve this answer



























                0














                As you said, you need
                1. Paging enabled
                2. Scrolling from left to right
                3. Multiple sections.



                It's a little bit confusing what exactly you want to design. It will be much better if can share proper UI screenshot.



                As I understand your problem.



                let layout = UICollectionViewFlowLayout()
                layout.scrollDirection = .horizontal
                let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)


                Check have you assign the horizontal layout to your collectionView.






                share|improve this answer

























                  0












                  0








                  0







                  As you said, you need
                  1. Paging enabled
                  2. Scrolling from left to right
                  3. Multiple sections.



                  It's a little bit confusing what exactly you want to design. It will be much better if can share proper UI screenshot.



                  As I understand your problem.



                  let layout = UICollectionViewFlowLayout()
                  layout.scrollDirection = .horizontal
                  let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)


                  Check have you assign the horizontal layout to your collectionView.






                  share|improve this answer













                  As you said, you need
                  1. Paging enabled
                  2. Scrolling from left to right
                  3. Multiple sections.



                  It's a little bit confusing what exactly you want to design. It will be much better if can share proper UI screenshot.



                  As I understand your problem.



                  let layout = UICollectionViewFlowLayout()
                  layout.scrollDirection = .horizontal
                  let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)


                  Check have you assign the horizontal layout to your collectionView.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 7:53









                  iDev750iDev750

                  6041417




                  6041417





















                      0














                      What I would do is this:



                      1) Sort your data the same way you do it now so that your collection view would look like this:



                      ---------
                      |0|2|4|6|
                      ---------
                      |1|3|5|7|
                      ---------


                      2) In your cellForIndexPath method of your collection view data source return the correct element so that your collection view turns into this:



                      ---------
                      |0|1|2|3|
                      ---------
                      |4|5|6|7|
                      ---------


                      To do that you can use this logic:



                      public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
                      var elementIndex: Int!
                      if indexPath.row % 2 == 0
                      // 0 / 2 = 0
                      // 2 / 2 = 1
                      // 4 / 2 = 2, etc
                      elementIndex = indexPath.row / 2

                      else
                      // (1 / 2) + round(8.0 / 2.0) = 4
                      // (3 / 2) + round(8.0 / 2.0) = 5
                      // (5 / 2) + round(8.0 / 2.0) = 6, etc
                      elementIndex = (indexPath.row / 2) + Int(round(Double(elements.count) / 2.0))


                      // Dequeue your cell
                      // let cell = ...

                      cell.element = elements[elementIndex]
                      return cell



                      Basically this should return your elements in the correct order.






                      share|improve this answer























                      • Hi, thanks for your help. Your 2nd solution half works, the only problem is that I have multiple pages in the collection view, and making the changes above, the collection view now reads like 0-1-2-3 4-5-6-7 8-9-10-11 on the top row, and 12-13-14-15 16-17-18-19 20-21-22-23 on the bottom row

                        – Adam Altinkaya
                        Dec 3 '18 at 11:22











                      • Well isn't that what it should read?

                        – Mihai Fratu
                        Dec 3 '18 at 15:49











                      • Oh! I just realized: you want to go 0-1-2-3 on the 1st row of 1st page, then 4-5-6-7 on the 2nd row of the 1st page, and then do the same for the 2nd page: 8-9-10-11 on the 1st row and 12-13-14-15 on the 2nd row of the 2nd page... Is this right? If so, yes, my solution does not do that :(

                        – Mihai Fratu
                        Dec 3 '18 at 15:51











                      • Yes that is the way I need it read! Ah no worries, thanks for your help!

                        – Adam Altinkaya
                        Dec 3 '18 at 16:13















                      0














                      What I would do is this:



                      1) Sort your data the same way you do it now so that your collection view would look like this:



                      ---------
                      |0|2|4|6|
                      ---------
                      |1|3|5|7|
                      ---------


                      2) In your cellForIndexPath method of your collection view data source return the correct element so that your collection view turns into this:



                      ---------
                      |0|1|2|3|
                      ---------
                      |4|5|6|7|
                      ---------


                      To do that you can use this logic:



                      public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
                      var elementIndex: Int!
                      if indexPath.row % 2 == 0
                      // 0 / 2 = 0
                      // 2 / 2 = 1
                      // 4 / 2 = 2, etc
                      elementIndex = indexPath.row / 2

                      else
                      // (1 / 2) + round(8.0 / 2.0) = 4
                      // (3 / 2) + round(8.0 / 2.0) = 5
                      // (5 / 2) + round(8.0 / 2.0) = 6, etc
                      elementIndex = (indexPath.row / 2) + Int(round(Double(elements.count) / 2.0))


                      // Dequeue your cell
                      // let cell = ...

                      cell.element = elements[elementIndex]
                      return cell



                      Basically this should return your elements in the correct order.






                      share|improve this answer























                      • Hi, thanks for your help. Your 2nd solution half works, the only problem is that I have multiple pages in the collection view, and making the changes above, the collection view now reads like 0-1-2-3 4-5-6-7 8-9-10-11 on the top row, and 12-13-14-15 16-17-18-19 20-21-22-23 on the bottom row

                        – Adam Altinkaya
                        Dec 3 '18 at 11:22











                      • Well isn't that what it should read?

                        – Mihai Fratu
                        Dec 3 '18 at 15:49











                      • Oh! I just realized: you want to go 0-1-2-3 on the 1st row of 1st page, then 4-5-6-7 on the 2nd row of the 1st page, and then do the same for the 2nd page: 8-9-10-11 on the 1st row and 12-13-14-15 on the 2nd row of the 2nd page... Is this right? If so, yes, my solution does not do that :(

                        – Mihai Fratu
                        Dec 3 '18 at 15:51











                      • Yes that is the way I need it read! Ah no worries, thanks for your help!

                        – Adam Altinkaya
                        Dec 3 '18 at 16:13













                      0












                      0








                      0







                      What I would do is this:



                      1) Sort your data the same way you do it now so that your collection view would look like this:



                      ---------
                      |0|2|4|6|
                      ---------
                      |1|3|5|7|
                      ---------


                      2) In your cellForIndexPath method of your collection view data source return the correct element so that your collection view turns into this:



                      ---------
                      |0|1|2|3|
                      ---------
                      |4|5|6|7|
                      ---------


                      To do that you can use this logic:



                      public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
                      var elementIndex: Int!
                      if indexPath.row % 2 == 0
                      // 0 / 2 = 0
                      // 2 / 2 = 1
                      // 4 / 2 = 2, etc
                      elementIndex = indexPath.row / 2

                      else
                      // (1 / 2) + round(8.0 / 2.0) = 4
                      // (3 / 2) + round(8.0 / 2.0) = 5
                      // (5 / 2) + round(8.0 / 2.0) = 6, etc
                      elementIndex = (indexPath.row / 2) + Int(round(Double(elements.count) / 2.0))


                      // Dequeue your cell
                      // let cell = ...

                      cell.element = elements[elementIndex]
                      return cell



                      Basically this should return your elements in the correct order.






                      share|improve this answer













                      What I would do is this:



                      1) Sort your data the same way you do it now so that your collection view would look like this:



                      ---------
                      |0|2|4|6|
                      ---------
                      |1|3|5|7|
                      ---------


                      2) In your cellForIndexPath method of your collection view data source return the correct element so that your collection view turns into this:



                      ---------
                      |0|1|2|3|
                      ---------
                      |4|5|6|7|
                      ---------


                      To do that you can use this logic:



                      public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
                      var elementIndex: Int!
                      if indexPath.row % 2 == 0
                      // 0 / 2 = 0
                      // 2 / 2 = 1
                      // 4 / 2 = 2, etc
                      elementIndex = indexPath.row / 2

                      else
                      // (1 / 2) + round(8.0 / 2.0) = 4
                      // (3 / 2) + round(8.0 / 2.0) = 5
                      // (5 / 2) + round(8.0 / 2.0) = 6, etc
                      elementIndex = (indexPath.row / 2) + Int(round(Double(elements.count) / 2.0))


                      // Dequeue your cell
                      // let cell = ...

                      cell.element = elements[elementIndex]
                      return cell



                      Basically this should return your elements in the correct order.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 23 '18 at 11:22









                      Mihai FratuMihai Fratu

                      6,90422951




                      6,90422951












                      • Hi, thanks for your help. Your 2nd solution half works, the only problem is that I have multiple pages in the collection view, and making the changes above, the collection view now reads like 0-1-2-3 4-5-6-7 8-9-10-11 on the top row, and 12-13-14-15 16-17-18-19 20-21-22-23 on the bottom row

                        – Adam Altinkaya
                        Dec 3 '18 at 11:22











                      • Well isn't that what it should read?

                        – Mihai Fratu
                        Dec 3 '18 at 15:49











                      • Oh! I just realized: you want to go 0-1-2-3 on the 1st row of 1st page, then 4-5-6-7 on the 2nd row of the 1st page, and then do the same for the 2nd page: 8-9-10-11 on the 1st row and 12-13-14-15 on the 2nd row of the 2nd page... Is this right? If so, yes, my solution does not do that :(

                        – Mihai Fratu
                        Dec 3 '18 at 15:51











                      • Yes that is the way I need it read! Ah no worries, thanks for your help!

                        – Adam Altinkaya
                        Dec 3 '18 at 16:13

















                      • Hi, thanks for your help. Your 2nd solution half works, the only problem is that I have multiple pages in the collection view, and making the changes above, the collection view now reads like 0-1-2-3 4-5-6-7 8-9-10-11 on the top row, and 12-13-14-15 16-17-18-19 20-21-22-23 on the bottom row

                        – Adam Altinkaya
                        Dec 3 '18 at 11:22











                      • Well isn't that what it should read?

                        – Mihai Fratu
                        Dec 3 '18 at 15:49











                      • Oh! I just realized: you want to go 0-1-2-3 on the 1st row of 1st page, then 4-5-6-7 on the 2nd row of the 1st page, and then do the same for the 2nd page: 8-9-10-11 on the 1st row and 12-13-14-15 on the 2nd row of the 2nd page... Is this right? If so, yes, my solution does not do that :(

                        – Mihai Fratu
                        Dec 3 '18 at 15:51











                      • Yes that is the way I need it read! Ah no worries, thanks for your help!

                        – Adam Altinkaya
                        Dec 3 '18 at 16:13
















                      Hi, thanks for your help. Your 2nd solution half works, the only problem is that I have multiple pages in the collection view, and making the changes above, the collection view now reads like 0-1-2-3 4-5-6-7 8-9-10-11 on the top row, and 12-13-14-15 16-17-18-19 20-21-22-23 on the bottom row

                      – Adam Altinkaya
                      Dec 3 '18 at 11:22





                      Hi, thanks for your help. Your 2nd solution half works, the only problem is that I have multiple pages in the collection view, and making the changes above, the collection view now reads like 0-1-2-3 4-5-6-7 8-9-10-11 on the top row, and 12-13-14-15 16-17-18-19 20-21-22-23 on the bottom row

                      – Adam Altinkaya
                      Dec 3 '18 at 11:22













                      Well isn't that what it should read?

                      – Mihai Fratu
                      Dec 3 '18 at 15:49





                      Well isn't that what it should read?

                      – Mihai Fratu
                      Dec 3 '18 at 15:49













                      Oh! I just realized: you want to go 0-1-2-3 on the 1st row of 1st page, then 4-5-6-7 on the 2nd row of the 1st page, and then do the same for the 2nd page: 8-9-10-11 on the 1st row and 12-13-14-15 on the 2nd row of the 2nd page... Is this right? If so, yes, my solution does not do that :(

                      – Mihai Fratu
                      Dec 3 '18 at 15:51





                      Oh! I just realized: you want to go 0-1-2-3 on the 1st row of 1st page, then 4-5-6-7 on the 2nd row of the 1st page, and then do the same for the 2nd page: 8-9-10-11 on the 1st row and 12-13-14-15 on the 2nd row of the 2nd page... Is this right? If so, yes, my solution does not do that :(

                      – Mihai Fratu
                      Dec 3 '18 at 15:51













                      Yes that is the way I need it read! Ah no worries, thanks for your help!

                      – Adam Altinkaya
                      Dec 3 '18 at 16:13





                      Yes that is the way I need it read! Ah no worries, thanks for your help!

                      – Adam Altinkaya
                      Dec 3 '18 at 16:13

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323564%2fuicollectionview-to-read-from-left-to-right-with-multiple-rows%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto