Hi Community I wanted to implement a UICollectionviewCell which have size of 110 and each row have 3 cells initially
1.) Now need to do some modification in such way firstRow should have 3 cells but the second should have 2 cells
2.) The first cells size should be 220 and second remains the same 110
3.) In the third row it will have same structure as first rows
4.) But in 4th row the first cell size will 110 and second will be 220
it will repeat in same structure for further data
I am able to design as 3 cells in each row but for rest I need help
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let width = (self.view.frame.size.width - 12 * 3) / 3
let height = width * 1.5
return CGSize(width: width, height: height)
}
One approach is to implement
sizeForItemAt.You can determine whether the row should have 2 or 3 cells by using the modulo operator (
%) like this:This will give you a 3-2-3-2-3... pattern.
Here's a quick example...
We'll use this cell class - a single label, constrained 8-points on all four sides:
Then this view controller class:
Which gives us this output: