gpt4 book ai didi

ios - UICollectionView 单元格根本没有间距

转载 作者:行者123 更新时间:2023-12-01 19:00:32 25 4
gpt4 key购买 nike

我有一个 Collection View ,它本身是委托(delegate)和数据源,并且布局定义如下:

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

结果是一个 Collection View ,其中每个部分中的项目根本没有任何间距。

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

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

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
}

关于ios - UICollectionView 单元格根本没有间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23208932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com