gpt4 book ai didi

具有不同单元类实例的 IOS UICollectionView 单元格

转载 作者:行者123 更新时间:2023-11-29 00:45:02 25 4
gpt4 key购买 nike

我正在尝试为 UiCollectionView 创建自定义单元格,我需要从后台更新每个单元格。

所以我为每个名为 Cell_Obj 的单元格创建了自定义类,并使用计时器从 Cell_Obj 本身更新单元格内容。

下面的代码在单元格上添加一个图像,并每 2 秒增加一个计数器,并将其显示在新的单元格标签上。

每次当我使用 viewcoroller 上的按钮添加新单元格时,单元格都会更新,但每个单元格在标签上都会获得相同的值。它假定具有不同的计数器值,因为每个单元格都是 Cell_Obj 的不同实例,但 counter 值和 labelTxt(单元格编号)具有相同的值每次定时器触发时的值。

ViewController.h

#import <UIKit/UIKit.h>
#import "Cell_Obj.h"

@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collection;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController (){

NSMutableArray *GridArray;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self.collection setDelegate:self];
[self.collection setDataSource:self];
// Do any additional setup after loading the view, typically from a nib.

GridArray = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return GridArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (Cell_Obj *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
Cell_Obj *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
// cell.label.text = @"123";

/*cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"AppIcon.png",indexPath.row]];*/
return cell;

}

- (void)addImage
{
Cell_Obj *cell = [[Cell_Obj alloc] init];

// cell.label.text = @"123";

//cell.label.text = [NSString stringWithFormat:@"%d",[dvrGridArray count]-1];
NSString * txt = [NSString stringWithFormat:@"%d",[GridArray count]];
[cell updateTextLabelName: txt];


[GridArray addObject:cell];
[_collection insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:[GridArray count]-1 inSection:0]]];


/* NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
[arrayWithIndexPaths addObject:cell];

[self.collection insertItemsAtIndexPaths:arrayWithIndexPaths];*/

}


- (IBAction)addClicked:(id)sender {

[self addImage];

}
- (IBAction)changeImage:(id)sender {

// DVR_Obj *cell = [dvrGridArray objectAtIndex:0];
// [cell changeImage ];
// [_collection reloadData];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
Cell_Obj *cell = [_collection cellForItemAtIndexPath:indexPath];
[cell changeImage ];

}

#pragma mark Collection view layout things
// Layout: Set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

//NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row);

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

CGSize mElementSize = CGSizeMake((screenWidth/4)-2, (screenHeight/4)-2);
return mElementSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}

// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right
return UIEdgeInsetsMake(1,1,1,1); // top, left, bottom, right
}

@end

Cell_Obj.h

#import <UIKit/UIKit.h>

@interface Cell_Obj : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *label;


- (void)changeImage;
- (void)updateTextLabelName:(NSString*)str;
@end

Cell_Obj.m

#import "Cell_Obj.h"

static NSString *labelTxt ;// = [[NSString alloc] init];
static int counter;

@implementation Cell_Obj{



}

+ (void)initialize {
if (self == [Cell_Obj class]) {
labelTxt = [[NSString alloc] init];
counter=0;

}
}


- (id)initWithFrame:(CGRect)frame
{

self = [super initWithFrame:frame];
if (self) {


}
return self;
}


- (void)awakeFromNib {

_imageView.image = [UIImage imageNamed:@"flower1.png"];

_label.text = labelTxt;

[NSTimer scheduledTimerWithTimeInterval:2.0f
target:self
selector:@selector(updateImage)
userInfo:nil
repeats:YES];
}


- (void)updateImage
{
_imageView.image = [UIImage imageNamed:@"AppIcon.png"];

counter++;
NSString * txt = [NSString stringWithFormat:@"%@-%d",labelTxt,counter];
_label.text = txt;
}

- (void)updateTextLabelName :(NSString*)str
{

labelTxt = str;
}



@end

最佳答案

您的每个单元格都不是新单元格。 Cell_Obj *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"forIndexPath:indexPath]; 此语句重复使用标识符为“Cell”的单元格。要每次都创建新单元格,请不要传递 indexPath,而是传递 nil

关于具有不同单元类实例的 IOS UICollectionView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38717300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com