This is how I did things in the end.
Upon tapping of specific segmented control I changed the layout and reloaded the cells.
- (void)displayTypeSegmentSelected
{
_selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];
if (_selectedDisplayTypeIndex == 0) {
NSLog(@"Single file item view selected");
[_collectionView setCollectionViewLayout:_flowLayout2 animated:YES];
[_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
} else if (_selectedDisplayTypeIndex == 1) {
NSLog(@"Grid style view selected");
[_collectionView setCollectionViewLayout:_flowLayout animated:YES];
[_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
}
}
In my cellForItemAtIndexPath
depending on which layout was present on screen I loaded a specific custom cell with correct settings.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
NSLog(@"collectionview 1 loaded");
static NSString *CellIdentifier = @"Cell";
VAGGarmentCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
static NSString *CellIdentifier2 = @"Cell2";
VAGGarmentCell2 *cell2 = [_collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier2 forIndexPath:indexPath];
if ([[_collectionView collectionViewLayout] isEqual: _flowLayout]) {
[[cell activityIndicator] startAnimating];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell imageView] setFile: userImageFile];
[[cell imageView] loadInBackground];
[[cell activityIndicator] stopAnimating];
[[cell title] setText:[object valueForKey:@"title"]];
[[cell price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell;
} else if ([[_collectionView collectionViewLayout] isEqual: _flowLayout2]) {
[[cell2 activityIndicator] startAnimating];
PFFile *userImageFile = [object valueForKey:@"image"];
[[cell2 imageView] setFile: userImageFile];
[[cell2 imageView] loadInBackground];
[[cell2 activityIndicator] stopAnimating];
[[cell2 title] setText:[object valueForKey:@"title"]];
[[cell2 price] setText:[NSString stringWithFormat: @"£%@ GBP", [object valueForKey:@"price"]]];
return cell2;
}
return 0;
//_addToFavouritesButton = [cell addFavouriteButton];
[_addToFavouritesButton addTarget:_thisController action:@selector(addToFavouritesButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
In my viewDidLoad here's where I reference the nib file for the custom cell for single item display.
// Grab cell nib file and give a reuse identifier
[_collectionView registerNib:[UINib nibWithNibName:@"VAGGarmentCell2" bundle:nil] forCellWithReuseIdentifier:@"Cell2"];
I get crashes but they're very hard to replicate, although every time I toggle between the different layouts my memory rises and rises. I'm guessing there's some old code that I need to remove. I'll sort this out tomorrow and check it out with instruments. However this is done and I can move on.
I may need to refactor the code where I repeat myself.