0

I am using UICollectionView with autolayout to display gallery. I have 2 types of prototype cells- one to display for 2-columned collectionView and one for 3-columned view.

On button click, the view is to be toggled to 2-columned and 3-columned

Following is my code:

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

I am not setting cell's height/width anywhere in code, I have just specified it in xib. But everytime 3 columned collectionView is displayed with about 10 pixel black gap between each cell.

I referred this link but it is using swift and custom cell classes. I don't want to use that.

How do I solve this? Where am I getting wrong?

Community
  • 1
  • 1
z22
  • 9,653
  • 17
  • 66
  • 122
  • if cell size varied means, you need to set cell size in collectio view, - (CGSize)collectionView:(UICollectionView *)collectionView layout:(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); } like this – NANNAV Dec 19 '15 at 10:57
  • @NANNAV tried this, still the same. Why do I need to set footer size? `referenceSizeForFooterInSection ` – z22 Dec 19 '15 at 11:06
  • have you checked the selected_layout value each time ? – sourav Dec 19 '15 at 11:08

1 Answers1

1

Add these delegate methods:

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