放置UICollectionView以创建2x3网格iOS的问题

时间:2013-11-23 16:47:00

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

我正在尝试将UICollectionView放置成每页2x3网格。这是我到目前为止所得到的:

enter image description here

正如你所看到的,下一个“页面”正在从右边偷窥,这是我不想要的。我想如果我知道我的宽度总是320pts(仅iPhone),那么我希望我的单元格在width中为130分,每边15分。

布局代码delegate

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

  return CGSizeMake(130, 130);
}

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

还有cellForItemAtIndexPath:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
                 cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CustCollectionViewCell *cell = 
      [collectionView dequeueReusableCellWithReuseIdentifier:kReuseID 
                                            forIndexPath:indexPath];

    //Set up list name label
    cell.LblListName.font = [self getDefaultFontWithSize:15.0f];
    cell.LblListName.textColor = fontAndTertiaryColor;
    cell.LblListName.textAlignment = NSTextAlignmentCenter;
    cell.LblListName.text = @"Failtron";
    //cell.LblListName.adjustsFontSizeToFitWidth = YES;

    cell.backgroundColor = secondaryColor;
    cell.parallaxIntensity = kParallaxMotion;
    [Utilities createViewBorder:cell];

    return cell;
}

我认为插页在padding中会像CSS一样,但我相信我与那个不合适,我还应该注意到UIStoryboard我的布局设置了流动。如果我遗漏了任何相关代码,请告诉我,这是我第一次尝试使用UICollectionView。我也想scroll horizontally

1 个答案:

答案 0 :(得分:1)

好的,有很多方法可以操纵UICollectionView的布局,但听起来好像使用“流”,你可以在<UICollectionViewDelegateFlowLayout>中实现.h,然后再使用以下方法根据您的喜好调整您的布局:

#pragma mark – UICollectionViewDelegateFlowLayout 

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

    // Size of the cell
    CGSize retval = CGSizeMake(130,130);
    return retval;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView 
                        layout:(UICollectionViewLayout*)collectionViewLayout 
        insetForSectionAtIndex:(NSInteger)section {

    //top, left, bottom, right
    return UIEdgeInsetsMake(0, 20, 0, 20);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView 
                   layout:(UICollectionViewLayout *)collectionViewLayout
            minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

    //Minimum spacing between cells
    CGFloat interimSpacing = 0.0f;
    return interimSpacing;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView 
                   layout:(UICollectionViewLayout *)collectionViewLayout 
           minimumLineSpacingForSectionAtIndex:(NSInteger)section {

    // Minimum spacing between lines. In your case, 
       this is the distance between columns
    // If your scroll direction was vertical, 
       this would be the distance between rows
    CGFloat lineSpacing = 20.0f;
    return lineSpacing;
}

我已经预先填充了这些代码,其中的一些值应该非常接近您想要完成的任务。

或者,所有这一切都可以在Interface Builder中完成,方法是选择UICollectionView并调整Size Inspector中的值,虽然我认为更加混乱。