2012-05-02 6 views
-1

私はいくつかの項目を含むNSArrayを持っています。 NSArrayは別のクラスのuitableviewにロードされます。詳細ビューから戻り、3回目の再入力を行うと、NSArrayは空になり、tableviewも空になります。何が起こっている?NSArrayは3回ビューが読み込まれた後にクリアします

- (void)viewDidLoad { 

    myMatch = [[Match alloc] initWithId:1 OpponentId:13]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *askCellIdent = @"askCell"; 
    static NSString *answerCellIdent = @"answerCell"; 


    if (indexPath.row == 0) 
    { 

     AskCell *cell = [tv dequeueReusableCellWithIdentifier:askCellIdent]; 
     if (cell==nil) { 
      cell = [[AskCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:askCellIdent]; 
     } 

     cell.nameLabel.text = @"Albert asked:"; 
     cell.questionLabel.text = [NSString stringWithFormat:@"%@", [[myMatch.chat objectAtIndex:indexPath.row] objectAtIndex:1]]; 
     return cell; 
    } 
    if (indexPath.row == 1) 
    { 

     AnswerCell *cell = [tv dequeueReusableCellWithIdentifier:answerCellIdent]; 
     if (cell==nil) { 
      cell = [[AnswerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:answerCellIdent]; 
     } 

     cell.nameLabel.text = @"Hugh answered:"; 
     cell.answerLabel.text = [NSString stringWithFormat:@"%@", [[myMatch.chat objectAtIndex:indexPath.row] objectAtIndex:1]]; 
     return cell; 
    } 

} 

を(私はそれが漏れているとは思わないので、私はアークを使用しています)ここに私のマッチクラスの初期化コードです:

- (id) initWithId:(NSInteger)yourId OpponentId:(NSInteger)opId { 
    self = [super init]; 
    if (self != nil) { 
     //set your id and opponents id to the match. 
     [self setUserId:yourId]; 
     [self setUserId:opId]; 

     JSON = @"[{\"opponentId\":\"4\",\"chat\":[[\"1\",\"Does he have a beard?\"],[\"1\",\"No.\"]]}]"; 

     //initiate the chat array. 
     chat = [[NSMutableArray alloc] init ]; 
     [self loadMatch]; 


    } 
    return self; 
} 

、ここでは、/

を合成チャットプロパティです
NSMutableArray *chat; 

@property (nonatomic, retain) NSMutableArray *chat; 

@synthesize chat; 

アレイのスロッグも空であるため、何が起こっているかわかりません!

+0

あなたは 'chat'にいつ値を追加しますか? – ThomasW

+0

初期化時に – michaela

+0

この行を置き換えてみてください:chat = [[NSMutableArray alloc] init]; with:self.chat = [NSMutableArray array]; – samfisher

答えて

0

私はそれが一度だけviewDidLoadと呼ばれるだろうとNSMutableArrayを取り込むに問題だと思うappDelegate

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    ypurAppDelegate *appDelegate = (yourAppDelegate *)[[UIApplication sharedApplication] elegate]; 


      appDelegate.repeatCount++; 

     if(appDelegate.repeatCount %3==0) 
     { 
      [array removeAllObjects]; 
      [tableview reloaddata]; 
     } 
} 
+0

私はコードに従いますが、これがどのように役立ち、なぜこの問題を解決するのですか/なぜそれが起こっていますか? – michaela

+0

appDelegate.repeatCount ++; (appDelegate.repeatCount%3 == 0)、3回目のロード時に[array removeAllObjects];を実行すると、ビューがロードされるたびに値が増えます。配列を空にする – Ranga

0

を使用してください。self.chat = [[[NSMutableArray alloc] init] autorelease];配列を初期化する

+0

は変更されません... – michaela

0

代わりに、ViewDidLoadオブジェクトをinitWithCoderに割り当てます。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

-(id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if(self) 
    { 
     myMatch = [[Match alloc] initWithId:1 OpponentId:13]; 
    } 
return self; 
} 
+0

は動作しません.... – michaela

0

のint varableのrepeatCountを宣言します。配列にviewWillAppearメソッドの値を挿入するか、一致クラスをinitWithCoderで開始する必要があります。

関連する問題