2015-12-19 21 views
0

我使用UICollectionView與自動佈局顯示畫廊。我有兩種類型的prototype cells - 一個用於顯示2列colourView和一個用於3-columned視圖。UICollectionView汽車的佈局,不同的細胞

在按一下按鈕,在視圖被切換到2圓柱和三圓柱

以下是我的代碼:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellGrid" forIndexPath:indexPath]; 
     if([selected_layout isEqualToString:@"tile"]){ 
      cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellTile" forIndexPath:indexPath]; // for 2 columns 
     } else { 
      cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellGrid" forIndexPath:indexPath]; // for 3 columns 
     } 
[cell layoutIfNeeded]; 
    [cell setNeedsDisplay]; 

    return cell; 
} 

我不設置單元格的高度/寬度在代碼的任何地方,我剛剛在xib中指定了它。但是每次顯示3個圓柱狀的collectionView時,每個單元之間的黑色間隙大約爲10個像素。

我提到this link,但它是用快速和自定義單元格類。我不想使用它。

我該如何解決這個問題?我哪裏錯了?

+0

如果單元格大小不同的方式,你需要在收藏品視圖,設置單元格大小, - (CGSize)的CollectionView:(UICollectionView *)的CollectionView 佈局:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section if([selected_layout isEqualToString:@「tile」]) return CGSizeMake(collectionView.frame.size.width/3,50); else return CGSizeMake(collectionView.frame.size.width/2,50); }這樣的 – NANNAV

+0

@NANNAV試過了,還是一樣的。爲什麼我需要設置頁腳大小? 'referenceSizeForFooterInSection' – z22

+0

您是否每次檢查selected_layout值? – sourav

回答

0

添加這些委託方法:

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


     return 5; 


} 


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