如何设置 UICollectionViewCell 大小

How to set UICollectionViewCell size

我使用 UICollectionView 并在单元格中设置图像。我想要的单元格大小是 width = view/2hight = view/3。和顶部 margin =8right = 8left = 8。我附上了一张图片,请看。我想要这个请看图片。

我的代码

- (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

我尝试了很多次,使用了更多的方法,但没有像附图那样设置单元格。请建议我该怎么做。谢谢

你试过了吗

- (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;
}

为您的 collection 视图提供左、右、上和下 0

使用以下 collection 视图的方法

- (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);}