2012-04-18 14 views
0

最近アプリケーションの開発を開始しましたので、私の無知を許してください。私はtableViewを持っていて、テーブルビューのセルがクリックされたときに、その真下に新しい行を挿入したいのです。これは現在私のコードで動作します。しかし、セルが再びクリックされると、挿入された行が削除されるようにしたい。これは私の配列の範囲外だと言っているNSRangeExceptionを私に与えています。iOS - ブレークポイントが無効になっている場合のみNSRangeException

これはおそらく私のtableView代理人/データメソッドと関係があると思いました。そのため、それぞれにブレークポイントを設定しました。ブレークポイントを有効にすると、セルは完全に削除されます。しかし、ブレークポイントを無効にしてアプリケーションを単独で実行させると、クラッシュします。ブレークポイントはどのようにこれに影響を与える可能性がありますか?

- (NSInteger) numberOfSectionsInTableView:(UITableView *)songTableView{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)songTableView 
numberOfRowsInSection:(NSInteger)section{ 
    bool debug = false; 

    if (debug) NSLog(@"--TableView: rankings"); 
    if (expandedRow == -1) 
     return [self.songs count]; 
    else //one row is expanded, so there is +1 
     return ([self.songs count]+1); 

} 

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

     bool debug = false; 
     if (debug) NSLog(@"--tableView: tableView"); 

     NSUInteger row = [indexPath row]; 
     if (row == expandedRow){ //the expanded row, return the custom cell 
      UITableViewCell *temp = [[UITableViewCell alloc] 
            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"test"]; 
      return temp; 
     } 
     UITableViewCell *cell = [tableViewCells objectAtIndex:row]; 
     return cell; 
    } 
} 

- (NSString *)tableView:(UITableView *)songTableView 
titleForHeaderInSection:(NSInteger)section{ 
     //todo: call refresh title 
     return @"The Fresh List"; 
} 

- (CGFloat)tableView:(UITableView *)songTableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     return 44.0; //same as SongCell.xib 

} 


- (void)tableView: (UITableView *)songTableView 
didSelectRowAtIndexPath: (NSIndexPath *)indexPath { 
    bool debug = true; 
    //todo: if the user selects expanded cell, doesn't do anything 
     SongCell *cell = (SongCell *)[songTableView cellForRowAtIndexPath:indexPath]; 
     if (cell->expanded == NO){ 
      //change cell image 
      cell.bgImage.image = [UIImage imageNamed:@"tablecellbg_click.png"]; 
      cell->expanded = YES; 

      //add new cell below 

      NSInteger atRow = [indexPath row] + 1; 
      NSIndexPath *insertAt = [NSIndexPath indexPathForRow:atRow inSection:0]; 
      NSArray *rowArray = [[NSArray alloc] initWithObjects:insertAt, nil]; 

      if (debug) NSLog(@"Expanded row: %d", atRow); 
      expandedRow = atRow; 

      [tableView insertRowsAtIndexPaths:rowArray withRowAnimation:UITableViewRowAnimationTop]; 

     }else { //cell is already open, so close it 
      //change cell image 
      cell.bgImage.image = [UIImage imageNamed:@"tablecellbg.png"]; 
      cell->expanded = NO; 

      NSIndexPath *removeAt = [NSIndexPath indexPathForRow:expandedRow inSection:0]; 
      NSArray *rowArray = [[NSArray alloc] initWithObjects:removeAt, nil]; 
      if(debug) NSLog(@"--about to delete row: %d", expandedRow); 

      expandedRow = -1; 
      [tableView deleteRowsAtIndexPaths:rowArray withRowAnimation:UITableViewRowAnimationTop]; 

      //remove expaned cell below 
     } 

} 

答えて

0

への呼び出しでテーブル構造を変更するコードをラップすることをお勧めします自分の質問に答えるが、私はそれを理解した。

オブジェクトの配列からtableViewをロードしていました。私がセルを追加したとき、その配列は30個のオブジェクトしか保持しませんでしたが、テーブルは31個保持されました。numberOfRowsInSectionメソッドを正しく返していました。

ここに私が行った変更があります。

NSUInteger row = [indexPath row]; 
     if (row == expandedRow){ //the expanded row, return the custom cell 
      if(debug) NSLog(@"row == expandedRow"); 
      UITableViewCell *temp = [[UITableViewCell alloc] 
            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"test"]; 
      return temp; 
     } 
     else if (expandedRow != -1 && row > expandedRow) 
      row--; 

私のオブジェクトの配列とUITableViewCellsは、1-1と一致すると考えられます。展開された行の後に、indexPathの行が1つずれています。この問題を解決するための良い方法があるとは思いますが、ここではこの問題の解決方法を示します。

0

私は、これはnullを返す賭け:[NSIndexPath indexPathForRow:expandedRow inSection:0]ここ

は、関連するコードです。それがない場合、それが吹くと...

HTH

+0

いいえ、これではなく、NSLogをメソッドの先頭に置きます。 DidSelectRowを呼び出す前にクラッシュしていることが分かります... – CHawk

1

は、これは単なる推測ですが、それは私が嫌い

[tableView beginUpdates]; 
[tableView endUpdates]; 
関連する問題