2011-01-10 10 views
0

ログイン処理が完了するとすぐにTableViewをリロードする必要があるアプリケーションを開発しています。テーブルデータがリロードされると、エラーEXC_BAD_ACCESSでアプリケーションがクラッシュします。それ以外のすべてのケースインスタンスを削除するとクラッシュしませんUITableViewがリロードされるとすぐにアプリケーションがクラッシュする

何が原因なのでしょうか?ここで

はコードです:

- (void)viewDidLoad { 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(loginDone:) 
                name:@"loginDone" object:nil]; 
    statTableView.backgroundColor = [UIColor clearColor]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) 
section { 

return 6; 
} 

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell. 

    switch (indexPath.row) { 

     case 0 : 

      cell.textLabel.text = @"Foo:"; 
      NSLog(@"%@", data7); 
      UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)]; 
      myLabel2.text = (@"%@", data7); 
      myLabel2.textColor = [UIColor blackColor]; 
      myLabel2.backgroundColor = [UIColor whiteColor]; 
      myLabel2.font = [UIFont fontWithName:@"Trebuchet MS" size:14]; 
      [cell.contentView addSubview:myLabel2]; 
      break; 

     case 1: 

      cell.textLabel.text = @"Foo: "; 

      UILabel *myLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)]; 
      myLabel4.text = (@"%@", data11); 
      myLabel4.textColor = [UIColor blackColor]; 
      myLabel4.backgroundColor = [UIColor whiteColor]; 
      myLabel4.font = [UIFont fontWithName:@"Trebuchet MS" size:14]; 
      [cell.contentView addSubview:myLabel4]; 
      break; 

     case 2: 

      cell.textLabel.text = @"Foo: "; 

      UILabel *myLabel8 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)]; 
      myLabel8.text = (@"%@", data3); 
      myLabel8.textColor = [UIColor blackColor]; 
      myLabel8.backgroundColor = [UIColor whiteColor]; 
      myLabel8.font = [UIFont fontWithName:@"Trebuchet MS" size:14]; 
      [cell.contentView addSubview:myLabel8]; 
      break; 

     case 3: 

      cell.textLabel.text = @"Foo: "; 


      UILabel *myLabel10 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)]; 
      myLabel10.text = [NSString stringWithFormat:@"%@", data4]; 

      if ([data4 isEqualToString:@"0"]) { 
       myLabel10.text = @"None"; 
      } 

      myLabel10.textColor = [UIColor blackColor]; 
      myLabel10.backgroundColor = [UIColor whiteColor]; 
      myLabel10.font = [UIFont fontWithName:@"Trebuchet MS" size:14]; 
      [cell.contentView addSubview:myLabel10]; 
      break; 

     case 4: 

      cell.textLabel.text = @"Foo: "; 


      UILabel *myLabel12 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 20, 30)]; 
      myLabel12.text = [NSString stringWithFormat:@"%@", data5]; 
      myLabel12.textColor = [UIColor blackColor]; 
      if ([data5 isEqualToString:@"Foo"]) { 
       myLabel12.textColor = [UIColor redColor]; 
       myLabel12.text = @"Nil"; 
      } 
      myLabel12.backgroundColor = [UIColor whiteColor]; 
      myLabel12.font = [UIFont fontWithName:@"Trebuchet MS" size:14]; 
      [cell.contentView addSubview:myLabel12]; 
      break; 

     case 5: 

      cell.textLabel.text = @"Foo: "; 


      UILabel *myLabel14 = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 30)]; 
      if ([data6 isEqualToString:@"Foo"]) { 
       myLabel14.textColor = [UIColor colorWithRed:(0/255.f) green:(100/255.f) blue:(0/255.f) alpha:1.0]; 
       myLabel14.text = @"No Dues"; 
      } else { 
       myLabel14.text = [NSString stringWithFormat:@"%@", data6]; 
       myLabel14.textColor = [UIColor redColor]; 
      } 
      myLabel14.backgroundColor = [UIColor whiteColor]; 
      myLabel14.font = [UIFont fontWithName:@"Trebuchet MS" size:14]; 
      [cell.contentView addSubview:myLabel14]; 
      break; 

      /* 
      [myLabel2 release]; 
      [myLabel4 release]; 
      [myLabel8 release]; 
      [myLabel10 release]; 
      [myLabel12 release]; 
      [myLabel14 release]; 
        */ 
    } 

    return cell;  
} 

答えて

0

コードにはいくつか問題があります。

まず、次のメソッドでは、自動解放UITableViewCellオブジェクトが必要です。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

第二に、セルは多分[のtableView dequeueReusableCellWithIdentifier:CellIdentifier]から来る、それはすでにあなたが追加したカスタムラベルを持っています。そして、この種のセルにカスタムラベルを追加します。この種のセルをサブクラス化し、プロパティで目的のUILabelを取得できます。

0

DATAX変数は何ですか?それらが解放され、nilに設定されていない場合、EXC_BAD_ACCESSの原因となる割り当て解除されたオブジェクトのメソッドを呼び出すことになります。

+0

いいえ、私はこれらのデータを公開しません。私はケース0のみを使用すると動作しません。インスタンスと、他のすべてのインスタンスを使用するとクラッシュする理由は何ですか? : –

+0

あなたは彼らがオートリリースされていないと確信していますか? – ughoavgfhw

+0

いいえ、そうではありません。 –

2

ここで基本的なデバッグを行う必要があります。まず、ブレークポイントを追加して、悪いexecが発生している行が見つかるまで、デバッガで行単位で移動します。いったんそれを持っていれば、すぐにその理由を理解することができます。

関連する問題