user 12345
user 12345

Reputation: 23

How to set UICollectionViewCell size

I have using a UICollectionView and set images in cells. I want a cell sizes is width = view/2 and hight = view/3. and top margin =8, right = 8, left = 8.I attached an image please see ones. I want to like this please see an image.

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

I tried to many times and many more methods are used but not set cell like attached image. please suggest how can I do this. Thankyou

Upvotes: 1

Views: 1362

Answers (2)

Chetan Hedamba
Chetan Hedamba

Reputation: 229

give left,right,top and bottom 0 to your collection view

use below method of collection view

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

Upvotes: 0

Rahul Patel
Rahul Patel

Reputation: 1832

Have you tried

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

Upvotes: 2

Related Questions