Ios UICollectionView每行显示3个项目
我有一个从故事板创建的UICollectionView 我希望视图中每行有3个项目。我通过以下方法做到了这一点:Ios UICollectionView每行显示3个项目,ios,objective-c,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios,Objective C,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,我有一个从故事板创建的UICollectionView 我希望视图中每行有3个项目。我通过以下方法做到了这一点: - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(col
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(collectionView.frame.size.width/3.6 , (collectionView.frame.size.height-100)/2);
}
但是,如果我有6个项目,那么我有2行,每个行有3个项目。
但当我有4个项目时,它将显示2行,每个行有2个项目
是否可以将它们显示为两行,第一行包含3个项目,第二行仅包含1个项目?在这里我做到了,我在UICollectionView上实现了UICollectionViewDelegateFlowLayout添加以下方法。 它工作,使用它
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
float cellWidth = screenWidth / 3.0; //Replace the divisor with the column count requirement. Make sure to have it in float.
CGSize size = CGSizeMake(cellWidth, cellWidth);
return size;
}
此方法的工作原理是屏幕大小宽度除以3
(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
float cellWidth = screenWidth / 3.2; //Replace the divisor with the column count requirement. Make sure to have it in float.
CGFloat screenHeight = screenRect.size.height;
float cellHeight = screenHeight/3.0;
CGSize size = CGSizeMake(cellWidth, cellHeight);
return size;
}
也应用以下代码-(ViewWillTransitionOnToSize)
(void)视图将传递大小:(CGSize)带有传递协调器的大小:(id)协调器{
[self.collectionView/*(这是我的集合视图名称)*/reloadData];
}
此代码将有助于在纵向模式和横向模式下获得相同数量的单元格。
我已经应用了上面的代码,在纵向模式下获得了3个单元格,在横向模式下获得了3个单元格。试试这个
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(self.view.frame.size.width/3 -10 , (collectionView.frame.size.height-100)/2);
}
我知道已经很晚了,但我目前使用的解决方案是使用KRLCollectionViewGridLayout,如下所示
KRLCollectionViewGridLayout* aFlowLayout = [[KRLCollectionViewGridLayout alloc]init];
aFlowLayout.aspectRatio = 1;
aFlowLayout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
aFlowLayout.interitemSpacing = 2;
aFlowLayout.lineSpacing = 2;
aFlowLayout.numberOfItemsPerLine = 3;
aFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.heigh) collectionViewLayout:aFlowLayout];
您可以使用以下命令找到KRLCollectionViewGridLayout库类
最重要的一点是,在collectionView的size inspector中,将单元格和行的最小间距设置为0。您需要首先计算内容的可用空间,然后将其除以每行要显示的项目数 下面是一个示例,假设
UICollectionView
占据了整个屏幕的宽度
private let numberOfItemPerRow = 2
private let screenWidth = UIScreen.main.bounds.width
private let sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
private let minimumInteritemSpacing = CGFloat(10)
private let minimumLineSpacing = CGFloat(10)
// Calculate the item size based on the configuration above
private var itemSize: CGSize {
let interitemSpacesCount = numberOfItemPerRow - 1
let interitemSpacingPerRow = minimumInteritemSpacing * CGFloat(interitemSpacesCount)
let rowContentWidth = screenWidth - sectionInset.right - sectionInset.left - interitemSpacingPerRow
let width = rowContentWidth / CGFloat(numberOfItemPerRow)
let height = width // feel free to change the height to whatever you want
return CGSize(width: width, height: height)
}
最简单的方法
Swift 5
flowLayout.aspectRatio = 1
flowLayout.sectionInset = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
flowLayout.interitemSpacing = 10
flowLayout.lineSpacing = 10
flowLayout.numberOfItemsPerLine = 4
flowLayout.scrollDirection = .vertical
self.collectionView.collectionViewLayout = flowLayout```
You can find KRLCollectionViewGridLayout library here -(https://github.com/klundberg/KRLCollectionViewGridLayout)
试试这个链接@PrashantTukadiya没有什么我可以用的,它仍然显示2行,每行2个项目,右边有一个空白(第一行有一个单元格)试试这个,你在故事板中为集合视图的单元格和行之间的间距设置了什么值?@AdeelMiraj 10每个项目只有4个项目,它将它们显示为2行,每行有2个单元格,我已经在这里尝试过了,CGSize size=CGSizeMake(100100)在give deault value中并尝试过。。(如果你可以在iphone 6或更高版本中试用,然后给出分辨率和设置大小的条件)这与手机无关,当我有6件物品时,一切都是完美的,但当我有4件物品时,收藏会将每一行的物品排列成相同的号码,我不知道这是否正常,或者我可以通过某种方式进行更改查看此链接:试试看
private let numberOfItemPerRow = 2
private let screenWidth = UIScreen.main.bounds.width
private let sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
private let minimumInteritemSpacing = CGFloat(10)
private let minimumLineSpacing = CGFloat(10)
// Calculate the item size based on the configuration above
private var itemSize: CGSize {
let interitemSpacesCount = numberOfItemPerRow - 1
let interitemSpacingPerRow = minimumInteritemSpacing * CGFloat(interitemSpacesCount)
let rowContentWidth = screenWidth - sectionInset.right - sectionInset.left - interitemSpacingPerRow
let width = rowContentWidth / CGFloat(numberOfItemPerRow)
let height = width // feel free to change the height to whatever you want
return CGSize(width: width, height: height)
}
UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)collectionView.collectionViewLayout;
flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
NSInteger itemRowCount = 3;
CGFloat rowWidth = collectionView.contentSize.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right;
CGFloat itemWidth = (rowWidth - flowLayout.minimumInteritemSpacing * (itemRowCount - 1) ) / itemRowCount;
CGFloat itemHeight = itemWidth / 3;
flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
flowLayout.aspectRatio = 1
flowLayout.sectionInset = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2)
flowLayout.interitemSpacing = 10
flowLayout.lineSpacing = 10
flowLayout.numberOfItemsPerLine = 4
flowLayout.scrollDirection = .vertical
self.collectionView.collectionViewLayout = flowLayout```
You can find KRLCollectionViewGridLayout library here -(https://github.com/klundberg/KRLCollectionViewGridLayout)