0

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

Lee Sugden
  • 63
  • 8

1 Answers1

1

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;
}
UditS
  • 1,936
  • 17
  • 37
  • Sorry but i am kinda new to this and i have only been learning for about a month, how exactly would i implement the size of my reuseIdentifiers within the code you have given, also do i need to add this code to my viewController file? Many Thanks in Advance. – Lee Sugden Jan 28 '16 at 21:48
  • Your *reuseIdentifiers* essentially describe which cell you want to display at runtime, right? So based on these reuseIdentifiers, you can return different sizes for different cells. Check the updated answer for sample code. And Yes, this would be added in your `GroupsViewController.m` – UditS Jan 28 '16 at 21:52
  • Totally off topic but you couldn't by any chance direct me in the right direction as to how to change the button icon on each click please. many Thanks in advance and your code worked perfectly! – Lee Sugden Jan 28 '16 at 22:05
  • 1
    You can set the `UIButton`'s `image` or `backgroundImage` using `[button setImage:[UIImage imageNamed:@"sample"] forState:UIControlStateNormal];` and `[button setBackgroundImage:[UIImage imageNamed:@"background"] forState:UIControlStateNormal];`. This can be added to IBAction called on button press. If you need further clarrification, please post it as a separate question :) – UditS Jan 28 '16 at 22:15
  • http://stackoverflow.com/questions/35073751/how-to-set-uibutton-image-on-click-three-tier – Lee Sugden Jan 28 '16 at 22:57