0

I custom collection view cell by cell' size.

In case I have 1 or 2 cell, it works perfectly, but when I have 3 cells, it doesn't work well.

Here is my code in case I have 3 cells:

case 3:
let lastPhoto = info.albumOtherPhoto.photos.first
let lastPhotoWidth = lastPhoto?.width
let lastPhotoHeight = lastPhoto?.height
var sizeForFistPhoto : CGSize!
var sizeForOtherPhotos: CGSize!
if lastPhotoWidth > lastPhotoHeight {
    sizeForFistPhoto = CGSizeMake(screenWidth, screenWidth * 0.5)
    sizeForOtherPhotos = CGSizeMake(screenWidth * 0.5, screenWidth * 0.5)
} else {
    sizeForFistPhoto = CGSizeMake(screenWidth * 0.5, screenWidth)
    sizeForOtherPhotos = CGSizeMake(screenWidth * 0.5, screenWidth * 0.5)
}
self.sizes.append(sizeForFistPhoto)
self.sizes.append(sizeForOtherPhotos)
self.sizes.append(sizeForOtherPhotos)

and CollectionViewDelegateFlowLayout:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return self.sizes[indexPath.row]
}

The result of my code is:

enter image description here

But I want it showing like this:

enter image description here

Is it possible to achieve this in code like I did (because I can achieve this in interface builder)?

halfer
  • 19,824
  • 17
  • 99
  • 186
Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116

1 Answers1

0

I fixed it by changing the Collection View Flow Layout in interface builder from vertical to horizontal

enter image description here

Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116