Kodeco Forums

Video Tutorial: Collection Views Part 8: Custom Layout

In this video we move on to creating our own custom layout breaking free of UICollectionViewFlowLayout.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3488-collection-views/lessons/9

I was confused a little by the following:

if column >= (numberOfColums - 1) {
   column = 0
} else {
   column = 1
}

I can see how it switches back and forth between the two columns, even though it uses the numberOfColums variable it does not work for more than two columns.

The following code also only works for two columns but does balance them depending on the height. (i.e. not just back and forth)

//if we are currently using the highest column switch to the other.
if contentHeight == CGRectGetMaxY(frame) 
  if column == 0 {
    column = 1
  } else {         
   column = 0
  }
}

Thanks for the series, I learned quite a lot

Paul

The example code is set to be two columns, so switching back and both is binary; it’s either column 0 or 1.

To set the number of columns Open MasterViewController.swift and inside viewDidLoad() set the numberOfColums = 3.

Then in WaterfallLayout.swift change the code like you did but set it to increment though the columns, like this:
column = column >= (numberOfColumns - 1) ? 0 : column + 1
In this case we check if we are on column 0, 1 or 2 and place the cell there.

(NB I misspelled numberOfColums in the example code. It should be numberOfColumns. You would need to refactor that throughout the example app.)