【问题标题】:Display just two columns, with multiple rows in a CollectionView using storyboard使用情节提要在 CollectionView 中仅显示两列,多行
【发布时间】:2016-11-18 14:05:39
【问题描述】:

我只想连续显示两个单元格,不管 iPhone 的屏幕尺寸是多少。喜欢,

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

UICollectionView 的情节提要设置是,

现在,当我在 6、5 秒或更短的时间内运行它时,只有一个单元格出现在一行中。我使用的代码是,

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


    【解决方案1】:

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

    注意:我已经回答了考虑到单元格是正方形大小的问题

    补充信息: 请确保您将估计大小设为 none https://i.stack.imgur.com/B9kmU.png

    【讨论】:

    • 感谢您拯救了我的一天。我太笨了,我什至没有想到这个简单的解决方案。非常感谢!
    • 这真的很有帮助。感谢您的回答和您提出的问题。
    • 这很好用。只需要在类中添加 UICollectionViewDelegateFlowLayout 即可。
    • 我的单元格不是方形的,单元格的宽度是它们高度的两倍,如何在我的收藏视图中有两列?
    • 非常感谢,最佳答案!对我帮助很大
    【解决方案2】:

    Xcode 8:Swift 3

    我知道这是一个较老的问题,但我发现一些问题与已接受的答案有关。

    我不得不使用 UICollectionViewDelegateFlowLayout 这不是 CollectionViewFlowLayout

    然后

    其中“填充”是 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)
    
       }
    
    }
    

    【讨论】:

      【解决方案3】:
      - (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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-14
        • 1970-01-01
        • 2019-09-29
        • 1970-01-01
        相关资源
        最近更新 更多