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:
But I want it showing like this:
Is it possible to achieve this in code like I did (because I can achieve this in interface builder)?