0

我在我的按钮中创建了一些代码来在我的按钮之间切换,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

自定义单元格.h

#import <UIKit/UIKit.h>

@interface CustomCell : UICollectionViewCell

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

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

@end

我认为这与我在中设置有关reuseIdentifier- (void)viewDidLoad因此我没有收到任何错误,因此我没有设置错误,所以我真正要求的是一种设置初始重用idzntifier并将其替换为以下内容的方法当我在按钮点击之间切换时。

如果有人能指出我在每次单击按钮时添加图标图像的正确方向,那也会很有帮助。

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

在此处输入图像描述

4

1 回答 1

1

据我了解,您UICollectionViewCell的 s 工作正常。您只需要在切换单元格时调整它们的大小。

- (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;
}
于 2016-01-28T21:38:24.827 回答