Search code examples
iosobjective-cuicollectionviewreuseidentifier

Toggle cell size button not changing cell identifier UIViewController


i have created some code in my button to toggle between my cell identifier of which does so pretty well but obviously i needed to set and initial cell identifier of which is small icon, so how would i go about remove that cell identifier and replacing it with another once the button is clicked. My current code is as follows:

GroupsViewController.m

#import "GroupsViewController.h"
#import "CustomCell.h"

@interface GroupsViewController ()
{
    NSArray *arrayOfImages;
    NSArray *arrayOfDescriptions;
}

@end

@implementation GroupsViewController
{
    NSString *reuseIdentifier;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self GroupsCollectionView]setDataSource:self];
    [[self GroupsCollectionView]setDelegate:self];
    reuseIdentifier= @"SmallIcon";


    arrayOfImages = [[NSArray alloc]initWithObjects:@"?.png", nil];

    arrayOfDescriptions = [[NSArray alloc]initWithObjects:@"?", nil];

}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [arrayOfDescriptions count];
}

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


    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    [[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
    [[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];

    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    //Dispose of any resources that can be recreated.
}

- (IBAction)cellToggleAction:(id)sender {

    if([reuseIdentifier isEqualToString:@"SmallIcon"])
            reuseIdentifier=@"ListView";
    else if
        ([reuseIdentifier isEqualToString:@"ListView"])
            reuseIdentifier=@"LargeIcon";
    else if
        ([reuseIdentifier isEqualToString:@"LargeIcon"])
            reuseIdentifier=@"SmallIcon";

    [self.GroupsCollectionView reloadData];
    }

@end

CustomCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UIImageView *IconImage;

@property (weak, nonatomic) IBOutlet UILabel *IconLabel;

@end

I assume its to do with me setting the reuseIdentifier in the - (void)viewDidLoad so that i didn't get any errors so that i hadn't set one, so really what i am asking for is a way to set the initial reuseidzntifier and replace it will the following when i toggle between the button clicks.

Also it would be helpful if someone could point me in the right direction as to adding icon images to each click of the button.

The problem happens when i am clicking the button as shown in the following images, the cells themselves change but the initial cell identifier stays put.

enter image description here


Solution

  • From what I understand your UICollectionViewCells are working fine. You just need to adjust their size when cells are toggled.

    - (CGSize)collectionView:(UICollectionView *)collectionView
                              layout:(UICollectionViewLayout *)collectionViewLayout
              sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
         CGSize cellSize;
    
         // Return required size based on your identifiers
        if([reuseIdentifier isEqualToString:@"SmallIcon"])
                cellSize = CGSizeMake(50, 50); // Sample size
        else if
            ([reuseIdentifier isEqualToString:@"ListView"])
                cellSize = CGSizeMake(80, 80); // Sample size
        else if
            ([reuseIdentifier isEqualToString:@"LargeIcon"])
                cellSize = CGSizeMake(120, 120); // Sample size
    
        return cellSize;
    }