仅显示两列,使用故事板在CollectionView中显示多行

raj*_*h s 21 storyboard ios uicollectionview uicollectionviewcell

我想连续显示两个单元格,无论iPhone的屏幕大小是多少.喜欢, 最终结果要求

我的故事板包含一个UICollectionView由约束连接的故事板.

故事板预览

故事板的设置UICollectionView是, 在此输入图像描述

现在,当我在6,5s或更低版本中运行时,只有一个单元格出现在一行中.我用的代码是,

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [categoryImages count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    homePageCells *cell = (homePageCells *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cells" forIndexPath:indexPath];
    cell.categoryName.text = [categoryNames objectAtIndex:indexPath.row];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用代码检测屏幕大小并以编程方式分配适当的单元格大小,

- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize sizes;


    CGSize result = [[UIScreen mainScreen] bounds].size;
    NSLog(@"%f",result.height);
    if(result.height == 480)
    {
        //Load 3.5 inch xib
        sizes =CGSizeMake(170.f, 170.f);
        NSLog(@"3gs");
    }
    else if(result.height == 568)
    {
        //Load 4 inch xib
        sizes =CGSizeMake(130.f, 130.f);
        NSLog(@"5s");

    }
    else if(result.height == 667.000000)
    {
        //Load 4.7 inch xib
        sizes =CGSizeMake(160.f, 160.f);
        NSLog(@"6");

    }
    else
    {
        NSLog(@"else");

        sizes =CGSizeMake(170.f, 165.f);

    }
    return sizes;
}
Run Code Online (Sandbox Code Playgroud)

但我也知道这不是正确的方法,所以请给我一个正确的方法来解决这个问题.

Roh*_*han 56

您可以这样做:您不需要检查设备大小,因为我们可以使用collectionView宽度来计算单元格的宽度并相应地更新我们的高度.

还有一件事:您需要使用UICollectionViewDelegateFlowLayout并确认下面的委托和实施方法

  func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

   let padding: CGFloat =  50
   let collectionViewSize = collectionView.frame.size.width - padding

   return CGSizeMake(collectionViewSize/2, collectionViewSize/2)

}
Run Code Online (Sandbox Code Playgroud)

更新:Swift 4.0

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let padding: CGFloat =  50
        let collectionViewSize = collectionView.frame.size.width - padding

        return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
    }
Run Code Online (Sandbox Code Playgroud)

注意:我已经回答了问题,因为细胞是方形的

  • 这非常有效.只需要在类中添加UICollectionViewDelegateFlowLayout即可. (2认同)

Rub*_*444 9

Xcode 8:Swift 3

我知道这是一个较老的问题,但我发现了一些接受答案的问题.

我不得不使用不是CollectionViewFlowLayout的UICollectionViewDelegateFlowLayout

然后

其中'Padding'是Int类型,当你尝试从变量collectionViewSize中减去它时,它会抛出一个错误,因为它们是不同的类型.但是很容易修复.

我刚补充说:CGFloat就行了

最后,尽管可能有更好的方法,但我必须通过调整collectionView前导和尾随约束来匹配填充.

总而言之,这就是我的代码最终看起来像

extension LandingPageViewController: UICollectionViewDelegateFlowLayout {

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let padding: CGFloat = 25
    let collectionCellSize = collectionView.frame.size.width - padding

  return CGSize(width: collectionCellSize/2, height: collectionCellSize/2)

   }

}
Run Code Online (Sandbox Code Playgroud)