添加新单元格时,集合视图单元格静态变量发生变化
Collection View Cell static variable changes when new cell added
我正在尝试使用 CollectionView 自定义单元格,我需要从集合视图单元格自定义 class 本身更新单元格。
这是自定义单元格-class
Cell_Obj.h
#import <UIKit/UIKit.h>
@interface Cell_Obj : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *label;
- (void)changeImage;
- (void)updateTextLabelName:(NSString*)str;
@end
Cell_Obj.m
#import "Cell_Obj.h"
static NSString *labelTxt ;
@implementation Cell_Obj{
}
+ (void)initialize {
if (self == [Cell_Obj class]) {
labelTxt = [[NSString alloc] init];
}
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)awakeFromNib {
_label.text = labelTxt;
[NSTimer scheduledTimerWithTimeInterval:2.0f
target:self
selector:@selector(updateLabel)
userInfo:nil
repeats:YES];
}
- (void)updateLabel
{
NSString * txt = [NSString stringWithFormat:@"%@",labelTxt];
_label.text = txt;
}
- (void)updateTextLabelName :(NSString*)str
{
labelTxt = str;
}
@end
我在 viewCotroller 中添加单元格的地方,
- (void)addCell
{
Cell_Obj *cell = [[Cell_Obj alloc] init];
NSString * txt = [NSString stringWithFormat:@"%d",[GridArray count]];
[cell updateTextLabelName: txt];
[GridArray addObject:cell];
[_collection insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:[GridArray count]-1 inSection:0]]];
}
上面代码的问题是,当我添加第一个单元格时,第一个单元格的标签为 0,这很好,但是当我添加第二个单元格时,计时器调用发生,单元格 1 和单元格 2 的标签值为 1。它应该分别有 0,1。
而且似乎单元格对象正在共享静态变量,因为在已创建的单元格上发生任何更新时,就像计时器事件一样。
为什么会这样,是不是我的做法有误?
请告诉我您的宝贵建议。
编辑
基于下面的回答,我已经将静态变量移动为实例变量,
@implementation Cell_Obj{
NSString *labelTxt ;
}
但在 updateLabel
labelTxt
里面是零。当我在 updateLabel
之前调试 updateTextLabelName
并且 labelTxt
具有正确的值时。
因为它是一个静态变量,所以它被所有单元实例共享。
使其工作的方法是从 labelTxt 定义中删除静态。
还有,它是静态的是什么意思?如果是定时器的原因,在更新之前检查一下定时器方法标签不为空,就可以解决你所有的问题。
这是因为 collectionview 对单元格进行了重新排序以提高内存效率。所以每次它都会调用 awakeFromNib
当它对单元格进行双端队列时。所以你应该使用集合视图 datasource methods
来更新或设置集合视图控件的内容。你应该实施 cellForItemAtIndexPath
来在你的标签中设置数据!!
我正在尝试使用 CollectionView 自定义单元格,我需要从集合视图单元格自定义 class 本身更新单元格。
这是自定义单元格-class
Cell_Obj.h
#import <UIKit/UIKit.h>
@interface Cell_Obj : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *label;
- (void)changeImage;
- (void)updateTextLabelName:(NSString*)str;
@end
Cell_Obj.m
#import "Cell_Obj.h"
static NSString *labelTxt ;
@implementation Cell_Obj{
}
+ (void)initialize {
if (self == [Cell_Obj class]) {
labelTxt = [[NSString alloc] init];
}
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)awakeFromNib {
_label.text = labelTxt;
[NSTimer scheduledTimerWithTimeInterval:2.0f
target:self
selector:@selector(updateLabel)
userInfo:nil
repeats:YES];
}
- (void)updateLabel
{
NSString * txt = [NSString stringWithFormat:@"%@",labelTxt];
_label.text = txt;
}
- (void)updateTextLabelName :(NSString*)str
{
labelTxt = str;
}
@end
我在 viewCotroller 中添加单元格的地方,
- (void)addCell
{
Cell_Obj *cell = [[Cell_Obj alloc] init];
NSString * txt = [NSString stringWithFormat:@"%d",[GridArray count]];
[cell updateTextLabelName: txt];
[GridArray addObject:cell];
[_collection insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:[GridArray count]-1 inSection:0]]];
}
上面代码的问题是,当我添加第一个单元格时,第一个单元格的标签为 0,这很好,但是当我添加第二个单元格时,计时器调用发生,单元格 1 和单元格 2 的标签值为 1。它应该分别有 0,1。
而且似乎单元格对象正在共享静态变量,因为在已创建的单元格上发生任何更新时,就像计时器事件一样。
为什么会这样,是不是我的做法有误?
请告诉我您的宝贵建议。
编辑
基于下面的回答,我已经将静态变量移动为实例变量,
@implementation Cell_Obj{
NSString *labelTxt ;
}
但在 updateLabel
labelTxt
里面是零。当我在 updateLabel
之前调试 updateTextLabelName
并且 labelTxt
具有正确的值时。
因为它是一个静态变量,所以它被所有单元实例共享。 使其工作的方法是从 labelTxt 定义中删除静态。
还有,它是静态的是什么意思?如果是定时器的原因,在更新之前检查一下定时器方法标签不为空,就可以解决你所有的问题。
这是因为 collectionview 对单元格进行了重新排序以提高内存效率。所以每次它都会调用 awakeFromNib
当它对单元格进行双端队列时。所以你应该使用集合视图 datasource methods
来更新或设置集合视图控件的内容。你应该实施 cellForItemAtIndexPath
来在你的标签中设置数据!!