仅显示两列,并使用情节提要在CollectionView中显示多行

问题描述 投票:23回答:5

无论iPhone屏幕尺寸如何,我都希望连续显示两个单元格。喜欢,End result required

我的情节提要中包含UICollectionView,由约束连接。

Storyboard preview

UICollectionView的情节提要设置为,enter image description here

现在,当我在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;
}

我尝试使用代码检测屏幕尺寸并以编程方式分配适当的像元尺寸,

- (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;
}

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

ios storyboard uicollectionview uicollectionviewcell
5个回答
62
投票

您无需检查设备尺寸,因为我们可以使用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)

}

更新: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)
    }

[Note:考虑到单元格是方形的,我已经回答了这个问题


10
投票

Xcode 8:Swift 3

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

我必须使用UICollectionViewDelegateFlowLayout,而不是CollectionViewFlowLayout

然后

在其中,'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)

   }

}

2
投票
- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat padding = 50;
CGFloat cellSize = collectionView.frame.size.width - padding;
return CGSizeMake(cellSize / 2, cellSize / 2);
}

0
投票

Xcode 11-Swift 4更新

基于:

将以下扩展添加到您的UICollectionViewController:

class MyCollectionController: UICollectionViewController {
    // your code...
}

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

      return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
  }
}

-1
投票

我当时传递给sizeForItemAtIndexPath的collectionView的边界不正确,因此出现问题。

对此的一种解决方法是重新加载viewWillAppear中的单元格,以确保collectionView的边界正确。

另一个是使用UIScreen类。

-(instancetype)init: {
    self = [super init];
    if (self) {
        width = [[UIScreen mainScreen] bounds].size.width;
    }
    return self;
}

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

    // Two columns with spacing in between
    CGFloat size = ((width / 2) - (kSpacing * 2));
    return CGSizeMake(size, size);

}

在这种情况下,我将委托作为一个单独的类实例化(因此初始化),但是它可以像扩展一样容易地完成。

© www.soinside.com 2019 - 2024. All rights reserved.