【问题标题】:UICollectionView alignment of cellsUICollectionView 对齐单元格
【发布时间】:2013-06-05 17:16:27
【问题描述】:

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

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

边缘插图是:

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

任何帮助将不胜感激。

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案1】:

    单元格的布局会根据它们的大小而有所不同,因此您需要为每种单元格类型使用不同的插图。我认为这应该是可行的:

    - (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 并尝试边缘插入,直到获得您想要的行为。

    【讨论】:

    • 谢谢,这是一个愚蠢的错误。我没有意识到,但居中的单元格太宽,无法在一行中有多个单元格。我已经减小了单元格的大小并解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 2014-04-27
    • 2018-11-14
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多