切换单元格大小按钮不更改单元格标识符UIViewController

时间:2016-01-28 21:16:51

标签: ios objective-c uicollectionview reuseidentifier

我已经在我的按钮中创建了一些代码,以便在我的cell identifier之间进行切换,但是很明显我需要设置并且初始单元格标识符是小图标,所以我将如何去除单元格标识符,单击该按钮后将其替换为另一个标识符。我目前的代码如下:

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

我假设它与我一起设置reuseIdentifier - (void)viewDidLoad所以我没有得到任何错误,所以我没有设置一个,所以我要求的是一种方法来设置初始的reuseidzntifier并替换它将在我按钮之间切换时点击次数。

如果有人能指出正确的方向,每次点击按钮都会添加图标图片,那将会很有帮助。

问题发生在我点击按钮时,如下图所示,单元格本身会发生变化,但初始单元格标识符会保持不变。

enter image description here

1 个答案:

答案 0 :(得分:1)

根据我的理解,您UICollectionViewCell的工作正常。您只需在切换单元格时调整其大小。

- (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;
}