Question

I've noticed I can create a crash in my app by quickly switching between two UICollectionViewFlowLayout's by tapping a segment of my UISegmentedControl.

The crash error I get is in this email:

enter image description here

In this method:

- (void)displayTypeSegmentSelected
{
    _selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];

    if (_selectedDisplayTypeIndex == 0) {
        NSLog(@"Single file item view selected");
        [_collectionView setCollectionViewLayout:_flowLayout2 animated:NO];
        [_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
    } else if (_selectedDisplayTypeIndex == 1) {
        NSLog(@"Grid style view selected");
        [_collectionView setCollectionViewLayout:_flowLayout animated:NO];
        [_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
    }
}

Properties:

@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"view did load");

    _thisController = self;
    _superView = [_thisController view];
    _collectionView = [_thisController collectionView];
    _navigationBar = [[_thisController navigationController] navigationBar];
    _selectedDisplayTypeIndex = 1; // default display type selected to grid style

    // Create flow layout
    _flowLayout2 = [[UICollectionViewFlowLayout alloc] init];

    // Set up margins, sizes etc
    [_flowLayout2 setHeaderReferenceSize:CGSizeMake(50,93)];
    [_flowLayout2 setItemSize:CGSizeMake(300, 500)];
    [_flowLayout2 setMinimumLineSpacing:0];
    [_flowLayout2 setMinimumInteritemSpacing:0];
    [_flowLayout2 setSectionInset:UIEdgeInsetsMake(1, 0, 40, 0)];
    [_flowLayout2 setScrollDirection:UICollectionViewScrollDirectionVertical];

    // Grab cell nib file and give a reuse identifier
    [_collectionView registerNib:[UINib nibWithNibName:@"VAGGarmentCell2" bundle:nil] forCellWithReuseIdentifier:@"Cell2"];

}

I ran the Analyzer and got 0 errors. I ran profiler and checked for memory leaks and there are none.

I read various other threads and found none that are as recent as mine. I've seen a lot of people experienced then when converting older projects to work with ARC. However mine has always used ARC (XCode 5/iOS7).

Any idea what could be going and how I could fix it?

Regards.

Was it helpful?

Solution

It's not clear to me why you would get this error if you change the layouts during scrolling, but you might be able to fix it by disabling your segmented control in scrollViewWillBeginDragging:, and re-enabling it in scrollViewDidEndDecelerating:. Make sure you set your controller to be the collection views' delegate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top