切换单元格大小按钮不更改单元格标识符 UIViewController
Toggle cell size button not changing cell identifier UIViewController
我已经在我的按钮中创建了一些代码来在我的 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 的方法,当我在按钮之间切换时将其替换为以下内容点击次数。
此外,如果有人能指出正确的方向,将图标图像添加到每次单击按钮,这将很有帮助。
问题发生在我单击下图所示的按钮时,单元格本身发生了变化,但初始单元格标识符保持不变。
据我了解,您的 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;
}
我已经在我的按钮中创建了一些代码来在我的 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 的方法,当我在按钮之间切换时将其替换为以下内容点击次数。
此外,如果有人能指出正确的方向,将图标图像添加到每次单击按钮,这将很有帮助。
问题发生在我单击下图所示的按钮时,单元格本身发生了变化,但初始单元格标识符保持不变。
据我了解,您的 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;
}