gpt4 book ai didi

ios - UICollectionView 对齐单元格

转载 作者:行者123 更新时间:2023-11-29 10:58:54 27 4
gpt4 key购买 nike

我有一个带有 2 种不同类型的 UICollectionViewCell 的 UICollectionView。有一个选项卡提示显示每种类型的单元格,一次只能显示一种类型的单元格。

出于某种原因,一种类型的单元格在 UICollectionView 中左对齐,但另一种单元格在中心水平对齐。我希望两种类型的单元格都左对齐。

边缘插图是:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(10,10,10,10);
}

如有任何帮助,我们将不胜感激。

最佳答案

单元格将根据其大小进行不同的布局,因此您需要为每种单元格类型使用不同的插图。我认为它应该像这样可行:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:section];
if ([cell isKindOfClass:[MyClass1 class]]){
return UIEdgeInsetsMake(10,10,10,10);
}else{
return UIEdgeInsetsMake(20,10,10,10);
}

用正确的类名替换 MyClass1 并试验边缘插入,直到获得所需的行为。

关于ios - UICollectionView 对齐单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16946048/

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