Xcode UICollectionview单元格左对齐

时间:2017-05-14 23:34:05

标签: ios objective-c xcode uicollectionview uicollectionviewcell

我有一个collectionview,每行包含3个单元格。每当连续不存在3个细胞时,细胞将对齐到中间。我希望他们左对齐。我该如何做到这一点?

下图显示了我的问题。如何将Cell 4对齐到左边?

enter image description here

这是我控制大小和插入的当前代码:

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

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenWidth = screenSize.width;
    CGFloat screenHeight = screenSize.height;

    return CGSizeMake(screenWidth/3-20, 80); //three cells per row

}

提前谢谢。

2 个答案:

答案 0 :(得分:0)

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CGRect screenBound  = [[UIScreen mainScreen] bounds] 
        float colum = 3.0;
     float spacing = 10.0;
    float value = floorf((Float(screenwith) - (colum - 1) * spacing) / 
     colum)

    return CGSizeMake(value, value); //three cells per row

}

答案 1 :(得分:0)

我知道它为时已晚,但是,我遇到了类似的问题,发现此解决方案可能会在将来对其他人有所帮助。

子类UICollectionViewFlowLayout并重写以下方法效果很好。

打开类LeftAlignedFlowLayout:UICollectionViewFlowLayout {

ArrayHelper::map(Investment::find()->where(['id_investment_type'=>[1,2,3]])->all,'id_investment','name','id_investment_type.name');

}