ios 如何在 UICollectionView 中每行显示 3 个单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33139996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display 3 cells per row in UICollectionView?
提问by FlySoFast
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: What did I miss? I've come across some other SO posts but no luck so far.
回答by Jo?e Ws
Swift 3.0. Works for both horizontal and vertical scroll directions and variable spacing
斯威夫特 3.0。适用于水平和垂直滚动方向和可变间距
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)
}
回答by Ashok R
//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.
有关更多信息,请访问以下链接。
回答by Arjun Patel
itemSpacing = "Spacing size between cell."
itemSpacing = "单元格之间的间距大小。"
itemsInOneLine = "Number of item which you want to display in single row."
itemsInOneLine = "要在单行中显示的项目数。"
collectionWidth = "Width of your collection view"
collectionWidth = "你的收藏视图的宽度"
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
let width = (collectionWidth) - itemSpacing * CGFloat(itemsInOneLine - 1)
layout.itemSize = CGSize(width:floor(width/CGFloat(itemsInOneLine)),height:width/CGFloat(itemsInOneLine))
回答by IOS developer
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.
您不必通过编码来完成。您只需要在 xib.as 中执行 iPhone 5 或 5s 宽度变为 320。并且您希望每行显示 3 个单元格,因此您必须执行 320/3 并且根据您得到的任何答案,您的单元格大小是根据结果。如果对我的回答有任何疑问,请问我。