1

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

- (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 之外,代码运行正确。

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

我设置的最小间距值应该向上调整,而不是向下调整,对吗?

那么为什么我看到的结果和这个屏幕上的一样呢?(我为每个收藏品上色,以便清楚地看到没有间距)

集合视图

有关更多详细信息,这里是我正在记录的帧,您可以看到第三个项目正好在第二个项目之后开始(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;
}
4

2 回答 2

3

您可以使用以下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
}
于 2014-04-22T04:10:17.783 回答
0

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

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

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

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

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

祝大家欢呼

于 2017-11-10T10:51:23.477 回答