昨日、テーブル ビューとテーブル ビュー内の各セルに固有の詳細ビューをリンクすることについて質問しました。ここで私の質問に対する良い答えを得たと思います。(うまくいけば、あなたはその投稿を読んで、私が必要なものを見ることができます). 基本的に、シングルトンを正しく作成しているかどうかを知りたかったのです。これが私のコードです:
timerStore.h
#import "Tasks.h"
@interface timerStore : NSObject
{
NSMutableDictionary *allItems;
}
+(timerStore *)sharedStore;
-(NSDictionary *)allItems;
-(NSTimer *)createTimerFor:(Tasks *)t inLocation: (NSIndexPath *)indexPath;
-(void)timerAction;
@end
timerStore.m
@implementation timerStore
+(timerStore *)sharedStore{
static timerStore *sharedStore = nil;
if (!sharedStore)
sharedStore = [[super allocWithZone:nil]init];
return sharedStore;
}
+(id)allocWithZone:(NSZone *)zone{
return [self sharedStore];
}
-(id)init {
self = [super init];
if (self) {
allItems = [[NSMutableDictionary alloc]init];
}
return self;
}
-(NSDictionary *)allItems{
return allItems;
}
-(NSTimer *)createTimerFor:(Tasks *)t inLocation: (NSIndexPath *)indexPath {
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:t.timeInterval target:self selector:@selector(timerAction) userInfo:nil repeats:1.0];
[allItems setObject:timer forKey:indexPath];
return timer;
}
-(void)timerAction{
//custom properties here
}
@end
下にスクロールすると (デキュー)、セルのインデックス パスが再利用されるという印象を受けていたので、ちょっと混乱しています。私は間違っているかもしれませんが。とにかく、リンクの男が示唆したように、私はシングルトンを作成する正しい道を進んでいますか?