Can Poyrazoğlu
Can Poyrazoğlu

Reputation: 34790

UICollectionViewFlowLayoutAutomaticSize EXC_BAD_ACCESS

I have a collection view and for some cells I want to return a specific height, and some others, I want to fall back to automatic size. Here is my code:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(something){
        return UICollectionViewFlowLayoutAutomaticSize;
    }else{
        return some CGSize
    }
}

However, I get EXC_BAD_ACCESS error when I try to return UICollectionViewFlowLayoutAutomaticSize. I've also tried to set the size to automatic dimension on flow layout itself:

UICollectionViewFlowLayout* layout = (UICollectionViewFlowLayout*)self.collectionViewLayout;
layout.itemSize = UICollectionViewFlowLayoutAutomaticSize;

I'm also getting the same error when I assign to UICollectionViewFlowLayoutAutomaticSize either.

What am I doing wrong?

Upvotes: 1

Views: 974

Answers (1)

Alexandre Bintz
Alexandre Bintz

Reputation: 693

Not sure if this is related to your problem, but I also had a EXC_BAD_ACCESS with a collection view with a flow layout. Turns out changing the .estimatedItemSize fixed the issue :

let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout

// this breaks
flowLayout.estimatedItemSize = CGSize(width: 100, height: 100)

// this does not break
flowLayout.estimatedItemSize = CGSize(width: 1, height: 1)

My cells have just a label, so they are smaller than 100x100. I guess it is not good if you give an estimated size that is larger than the actual size...

Hope this helps anyone.

Upvotes: 2

Related Questions