gpt4 book ai didi

ios - 每行 3 个项目的 UICollectionViewCell 大小

转载 作者:行者123 更新时间:2023-11-28 23:58:01 24 4
gpt4 key购买 nike

我正在尝试创建一个每行包含 3 个图像的 UICollectionView

一开始我使用了这段代码:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(110, 110);
}

它看起来不错,但它只适合 iPhone X 屏幕: enter image description here

然后我尝试使用这段代码,这样每行将放置 3 个单元格:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
float cellWidth = screenWidth / 3.0;
CGSize size = CGSizeMake(cellWidth, cellWidth);

return size;
}

但是随后 View 开始看起来不一样了: enter image description here

这是单元初始化方法:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"TrendingCell";

TrendingCell *cell = (TrendingCell*)[collection dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

TrendingItem * item = [[[TrendingRep sharedTrending] trendingArray] objectAtIndex:indexPath.row];

cell.text.text = item.title;

cell.image.layer.cornerRadius = cell.image.frame.size.width / 2;
cell.image.clipsToBounds = YES;

[cell.image sd_setImageWithURL:[NSURL URLWithString:item.imageUrl]
placeholderImage:nil
options:SDWebImageProgressiveDownload
completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {

[cell.progressView setHidden:NO];
}];

return cell;
}

知道 View 可能有什么问题吗?

最佳答案

您似乎没有考虑布局的 minimumInteritemSpacing。计算项目宽度的公式应该是

float cellWidth = (screenWidth - 2.0 * minimumInteritemSpacing) / 3.0;

关于ios - 每行 3 个项目的 UICollectionViewCell 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50550332/

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