2015-10-07 55 views
1

我正在使用uicollection视图来显示网格视图格式的图像。我能够显示单元格,但高度为&宽度不增加的iPad它只显示为小块。请告诉我如何使它动态。如何在iOS中为uicollection view cell制作动态大小?

[![在这里输入的形象描述] [1] [1]

我已经使用了如下使动态代码。

编译标志Collection视图布局事情

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 
    if (collectionView==self.album_collection_view) { 
     return 2.0; 
    } 
    else 
     return 2.0; 
} 

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { 
    if (collectionView==self.album_collection_view) { 
     return 2.0; 
    } 
    else 
     return 2.0; 
} 
// Layout: Set Edges 
- (UIEdgeInsets)collectionView: 
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { 
    // return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right 
    if (collectionView==self.album_collection_view) { 
     return UIEdgeInsetsMake(5,0,0,0); 
    }// top, left, bottom, right 
    else 
     return UIEdgeInsetsMake(5,0,0,0); 

} 

请告诉我,我怎样才能解决这个问题呢?

回答

-1

如果您想增加单元格高度和宽度,您必须使用sizeForItemAtIndexPath方法。为此,您必须添加UICollectionViewDelegateFlowLayout委托人。

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

它会为所有设备制作dynmaic尺寸。 – Techiee

+0

如果你想在所有设备的每一行显示4单元格,然后通过'返回CGSizeMake(collectionView.frame.size.width/4,collectionView.frame.size.width/4);'所以在所有设备按照你的collectionview宽度将减少并增加您的单元格高度和宽度。如果你想在特定的设备上显示一些特定的高度和宽度,那么你必须检查设备,如果它的5s,6或ipad,并根据您的需要指定高度。 –

1

这里是解决

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

YourCollectionCell * cell = (YourCollectionCell *) [YourCollectionViewObj cellForItemAtIndexPath:indexPath]; 

     if (cell == nil) { 

      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"YourCollectionCell" owner:self options:nil]; 
      cell = [topLevelObjects objectAtIndex:0]; 
      cell.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 20); 
      // SET YOUR CONTENT 
      [cell layoutIfNeeded]; 

     } 

     CGSize CellSize = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize withHorizontalFittingPriority:UILayoutPriorityDefaultHigh verticalFittingPriority:UILayoutPriorityDefaultLow]; 


     return CellSize;} 

如果上述方案不能满足您的需要,为了得到你想要的东西,你应该创建自己的UICollectionViewLayout(或者UICollectionViewFlowLayout)的子类,在这里执行所有将每个项目放在正确的框架中所需的计算。

看看这里的great tutorial和这里为something similar to what you want

+0

什么是YourCollectionViewObj? – Techiee

+0

@deepakkumar您的收藏视图 –

+0

其对象我没有为我收集View容器任何separte笔尖 – Techiee

相关问题