Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

Receive an error customizing collectionView: EXC_BAD_ACCESS code=2

I have an a collection view that get data from an instagram.

CollectionView is hooked through an Outlet:

@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

There is how i fill cells:

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    myCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];

    if(!cell){

        cell = (myCell *)[myCell cell];
    }

    [self customizeLayoutFlow];
    cell.photo = self.photos[indexPath.row];


    return cell;
}

-(void)customizeLayoutFlow{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.myCollectionView.collectionViewLayout = layout;
    layout.itemSize = CGSizeMake(106.0, 106.0);
    layout.minimumInteritemSpacing = 1.0;
    layout.minimumLineSpacing = 1.0;
}

Before you ask, i want to add, that cells load perfectly without customizeLayoutFlow method. But i need to customize its layout with UICollectionViewFlowLayout. For some reason i got an EXC_BAD_ACCESS code=2 error pointing at self.myCollectionView.collectionViewLayout = layout;.

Before there was no data, collectionView just load empty cells but with design i want (means customizeLayoutFlow works well).

How to fix that issue?

Update:

I fix problem with another way:

#pragma mark - customize layout

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

    return CGSizeMake(106.0, 106.0);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

    return 1.0;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

    return 1.0;
}

So sad i didn't figure out how to use UICollectionViewFlowLayout but at least it works.

Upvotes: 0

Views: 368

Answers (1)

Andrey Chernukha
Andrey Chernukha

Reputation: 21808

What if you try:

-(void)customizeLayoutFlow{

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

layout.itemSize = CGSizeMake(106.0, 106.0);
layout.minimumInteritemSpacing = 1.0;
layout.minimumLineSpacing = 1.0;

self.myCollectionView.collectionViewLayout = layout;
}

instead?

Upvotes: 1

Related Questions