0

我已經使用UICollectionView並在單元格中設置圖像。我想要的單元格大小是width = view/2hight = view/3。和頂部margin =8,right = 8,left = 8。我附上一張圖片請參閱。我想喜歡這個,請看一張圖片。如何設置UICollectionViewCell大小

MY CODE

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
return img_ary.count; 
} 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *identifier = @"Cell"; 


category_cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 
cell.img_view.image = [UIImage imageNamed:[img_ary objectAtIndex:indexPath.row]]; 


return cell; 
} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 
// [self performSegueWithIdentifier:@"showRecipeDetails" sender:self]; 


} 

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ 
return UIEdgeInsetsMake(0, 0, 0, 0); //top, left, bottom, right); 

} 

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 
CGFloat widthOfCell =(self.view.frame.size.width-30)/2; 
CGFloat heightOfCell =(self.view.frame.size.height-30)/4; 
CGSize returnValue = CGSizeMake(widthOfCell, heightOfCell); 
returnValue.height += 5; 
returnValue.width += 5; 
return returnValue; 

} 

IMAGE: I want to likes this please see

我試圖多次,有更多的方法用於但不設置像附加圖像細胞。請建議我如何做到這一點。三江源

回答

1

你試過

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

    CGSize defaultSize = CGSizeMake((_collectionViewSongs.frame.size.width/2)-10, (_collectionViewSongs.frame.size.width/2)-10); 


    return defaultSize; 
} 
+0

如何在兩個小區之間設定間距? –

+0

使用edgeInset。給UIEdgeInsetsMake(0,5,0,5); –

0

給予左,右,上,下0到您的收藏視圖

使用下面集合視圖的方法

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ 
int width = (collectionView.frame.size.width/2)-8; 
int height = (collectionView.frame.size.height/3)-8; 
return CGSizeMake(width, height);} 
+0

如何設置兩個單元格的間距? –

+0

選擇你的collectionview,並找到尺寸檢查員,找到「Min Spacing」for Cell and for Line。你可以從這裏改變間距 –