2012-04-18 98 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切入口:0];如果確實如此它吹...

心連心

+0

不,不是,我把NSLog放在方法的頂部。事實證明,它甚至在調用DidSelectRow之前崩潰了...... – CHawk 2012-04-18 23:54:00

1

這只是一個猜測,但它的包裝代碼,改變調用表結構

[tableView beginUpdates]; 
[tableView endUpdates];