切换单元格大小按钮不更改单元格标识符UIViewController(Toggle cell size button not changing cell identifier UIViewContro

编程入门 行业动态 更新时间:2024-10-25 09:38:35
切换单元格大小按钮不更改单元格标识符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

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

此外,如果有人能指出我正确的方向,为每次点击按钮添加图标图像,将会很有帮助。

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

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.

最满意答案

据我所知,您的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; }

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

更多推荐

本文发布于:2023-08-04 09:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1414096.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   标识符   按钮   大小   UIViewController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!