コレクションビューのセルカスタムクラス自体からセルを更新する必要があるCollectionViewカスタムセルを使用しようとしています。ここでコレクションビュー新しいセルが追加されたときにセルの静的変数が変更される
はカスタムセルクラスはviewCotrollerに私が
、のようにセルを追加してい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
です
- (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であり、それは問題ありませんが、2番目のセルを追加するとタイマー呼び出しが発生します。それはそれぞれ0,1を持っているはずです。
これは、タイマーイベントのように、既に作成されたセルで発生した更新時に、セルオブジェクトが静的変数を共有しているようです。
なぜこれが起こっているのですか、私のアプローチに間違いがありますか?
貴重なご提案をお知らせください。
私はインスタンス変数などの静的変数を移動してきた答えの下に基づいて編集
、
@implementation Cell_Obj{
NSString *labelTxt ;
}
が、内部
updateLabel
labelTxt
はnilです。どこで
updateTextLabelName
をデバッグするときに
updateLabel
の前に呼び出され、
labelTxt
が正しい値を持っています。
私はいくつかの参考資料を提供できますか? – CodeDezk
[https://www.appcoda.com/ios-programming-uicollectionview-tutorial/](https://www.appcoda.com/ios-programming-uicollectionview-tutorial/)を参照してください。 – Lion
私がここに投稿した質問http://stackoverflow.com/questions/38748949/ios-declare-boost-class-as-class-variable – CodeDezk