繁体   English   中英

UICollectionView单元格根本不是间距

[英]UICollectionView cells not spacing at all

我有一个集合视图,它是自己的委托和数据源,并且具有如下定义的布局:

- (id)initWithCoder:(NSCoder *)aDecoder{ 
    self = [super initWithCoder:aDecoder];
    if (self){
        self.dataSource = self;
        self.delegate = self;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(0, 8, 0, 8);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.footerReferenceSize = CGSizeMake(1, 70);
        layout.itemSize = CGSizeMake(60, 70);
        layout.minimumInteritemSpacing = 4.0f;
        [self setCollectionViewLayout:layout];
    }
    return self;
}

除了minimuminteritemspacing之外,代码运行正常。

结果是一个集合视图,其中每个部分中的项目根本没有任何间距。

我设定的最小间距值应向上调整,而不是向下调整,是否正确?

那么为什么我看到的结果就像这个屏幕中的那个? (我为每个收集品着色,以便明确没有间距)

的CollectionView

有关更多详细信息,这里是我正在记录的帧,因为您可以看到第三个项目正好在第二个项目之后(145像素= 85(previousitem.x)+ 60(previousitem.width))没有间距。

2014-04-22 10:25:49.954 App[15172:60b] self <PhotoIndexCollectionViewCell: 0x16576630; baseClass = UICollectionViewCell; frame = (8 0; 60 70); layer = <CALayer: 0x165777a0>>

2014-04-22 10:25:49.955 App[15172:60b] self <PhotoIndexCollectionViewCell: 0x16578380; baseClass = UICollectionViewCell; frame = (85 0; 60 70); layer = <CALayer: 0x16578520>>

2014-04-22 10:25:49.955 App[15172:60b] self <PhotoIndexCollectionViewCell: 0x16586620; baseClass = UICollectionViewCell; frame = (145 0; 60 70); layer = <CALayer: 0x165866c0>>

我在这里误解了什么?

我甚至尝试添加这个实际为每个部分调用的方法,但结果是一样的。

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4.0f;
}

您可以使用以下Delegate方法来设置间距:

// Layout: Set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

 //    NSLog(@"SETTING SIZE FOR ITEM AT INDEX %ld", (long)indexPath.row);
       CGSize mElementSize = CGSizeMake(150, 36);
       return mElementSize;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
       return 3.0;
}

 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
      return 7.0;
  }

// Layout: Set Edges
 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
   // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
      return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
}

如苹果文档中所述,空间可能会向上调整 - 但它看起来也可能向下调整。 老实说,我真的不明白为什么,也没有时间去调查这个问题(我正面临问题,必须完成我的工作;)) - 这可能只是Apple布局中的一个错误,因为它被提出是如此简单

https://developer.apple.com/documentation/uikit/uicollectionviewflowlayout/1617706-minimuminteritemspacing?changes=latest_minor

无论如何,我看到两种解决方法:

1 - 通过添加所有项宽度(如果宽度不固定)或将其设置为(itemWidth + minimumSpace)* numberOfItems + minimumSpace(用于第一个填充)来计算集合容器边界(在viewDidLayout函数中?)

2 - 激进的方式(我选择的方式):我将我的单元格内容嵌入到我想要的数量的视图中。 它不能失败。

干杯全都

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM