How to display 3 cells per row in UICollectionView?

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to display 3 cells per row in UICollectionView?



I used a custom flow layout according to this post.
Here is my implementation:


@implementation CustomLayout
-(void)prepareLayout{
[super prepareLayout];
// [self invalidateLayout];
if(self.collectionView){
CGSize newItemSize=self.itemSize;

// Number of items per row
int itemsPerRow=3;

float totalSpacing=self.minimumLineSpacing*(itemsPerRow-1);

newItemSize.width=(self.collectionView.bounds.size.width -totalSpacing)/itemsPerRow;

if(self.itemSize.height>0){
float itemAspectRatio=self.itemSize.width/self.itemSize.height;
newItemSize.height=newItemSize.width/itemAspectRatio;
}

[self setItemSize:newItemSize];

}


}
@end



This is what I've got: Result
What did I miss? I've come across some other SO posts but no luck so far.




4 Answers
4



Swift 3.0. Works for both horizontal and vertical scroll directions and variable spacing



Declare number of cells you want per row


let numberOfCellsPerRow: CGFloat = 3



Configure flowLayout to render specified numberOfCellsPerRow


flowLayout


numberOfCellsPerRow


if let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
let horizontalSpacing = flowLayout.scrollDirection == .vertical ? flowLayout.minimumInteritemSpacing : flowLayout.minimumLineSpacing
let cellWidth = (view.frame.width - max(0, numberOfCellsPerRow - 1)*horizontalSpacing)/numberOfCellsPerRow
flowLayout.itemSize = CGSize(width: cellWidth, height: cellWidth)
}





worked excellently! xcode 8.3.2, swift 3
– agrippa
Sep 1 '17 at 6:47





Thanks worked :) Xcode 9, swift 4
– Yakup Ad
Oct 23 '17 at 12:36





Thanks Brother . It is working.
– veerendra pratap singh
Mar 22 at 8:27





Tested this on a few different devices, but not all. Fails on iPad Air, iPad 5th Gen, both running iOS 11.2. Xcode 9.2, Swift 4. I make this call in viewDidLoad().
– xdeleon
Apr 4 at 3:16


//Make use of UICollectionViewDelegateFlowLayout Protocol

class RVC: UICollectionViewController {
//some code
}

extension RVC: UICollectionViewDelegateFlowLayout{

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
var collectionViewSize = collectionView.frame.size
collectionViewSize.width = collectionViewSize.width/3.0 //Display Three elements in a row.
collectionViewSize.height = collectionViewSize.height/4.0
return collectionViewSize
}



For more information go through below link.



UICollectionView Set number of columns





Tested this on iPhone X and iPhone SE. Both running 11.2, with Xcode 9.2/Swift 4. On both devices it did not work.
– xdeleon
Apr 4 at 3:22



itemSpacing = "Spacing size between cell."



itemsInOneLine = "Number of item which you want to display in single row."



collectionWidth = "Width of your collection view"


let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
let width = (collectionWidth) - itemSpacing * CGFloat(itemsInOneLine - 1)
layout.itemSize = CGSize(width:floor(width/CGFloat(itemsInOneLine)),height:width/CGFloat(itemsInOneLine))



You don't have to do it by coding. You just have to do in xib.as per iPhone 5 or 5s width become 320. and you want to show 3 cells per row so u have to do 320/3 and as per you get whatever answer your cell size is as per result.If any doubt of my answer ask me.





then how about larger screens like iPhone 6 plus or iPad? My app tends to run on many devices.
– FlySoFast
Oct 15 '15 at 4:57





so you have to do like self.view.frame.size.width/3 for any type of device width.
– IOS developer
Oct 15 '15 at 4:58





you just have to calculate only for two device iPhone and iPad.
– IOS developer
Oct 15 '15 at 5:00





for iphone 6 and iphone 6 plus you just have to remove launch image source.
– IOS developer
Oct 15 '15 at 5:03





well..basically that is what I was doing in the code above: calculate the width and with superview' width and the spaces between the cells.
– FlySoFast
Oct 15 '15 at 5:31






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ilG29o
3RMKpaTmflGbKF5xiuRVBKo8t7Gof3g,d,z41sQvKq 1OXml AZYarT,CEYt,XlZcrP9qOqZuba2kns8,aEDzNTXq

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham