2012-01-29 49 views
0

我有一個導航控制器,包含一個uitableview,當我按下一行時,它彈出一個新的視圖控制器在堆棧上,它用於顯示詳細信息在詳細信息視圖中,它從服務器發出請求到獲取一些響應信息,然後一旦返回信息,我使用insertRowsAtIndexPaths:來顯示從服務器返回的信息。UITableView insertRowsAtIndexPaths:

這一切工作正常的第一次,然後當我按下返回按鈕,然後選擇一個新的行或同一行查看詳細信息,一旦我的insertRowsAtIndexPaths:叫我收到以下錯誤:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

這裏是用於推動堆棧上的視圖的代碼:一旦該信息被從服務器返回時執行的代碼

VideoDetailViewController_iPhone *nextView = [[VideoDetailViewController_iPhone alloc] initWithNibName:@"VideoDetailViewController_iPhone" bundle:nil withVideo:rowData]; 
    nextView.navController = navController; 
[navController pushViewController:nextView animated:YES]; 
[nextView release]; 

這裏

- (void)fetchVideoDetail:(NSNotification *)notification { 
     hasLoadedResponses = YES; 

     NSArray *obj = (NSArray *)[notification object]; 
     responses = [[obj valueForKey:@"responses"] mutableCopy]; 
     //NSLog(@"RESPONSES: %@", responses); 
     if ([responses count] == 0) { 
      [tblView reloadData]; 
      return; 
     } 

     NSMutableArray *indexes = [[NSMutableArray alloc] init]; 

     int i = 0; 
     for (NSArray *x in responses) { 
      if (i > 0) { 
       //The reason for skipping the first one is because we will change that row once the table refreshes we just need to insert any rows after the first one. 
       [indexes addObject:[NSIndexPath indexPathForRow:i inSection:1]]; 
      } 
      i++; 
     } 
     //NSLog(@"indexCount: %i", [indexes count]); 
     [tblView beginUpdates]; 
     [tblView insertRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationBottom]; 
     [tblView endUpdates]; 
     //[tblView reloadData]; 
    } 

這裏是的tableView方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    if (section == 0) { 
     return 1; 
    } else { 
     if ([responses count] == 0) { 
      NSLog(@"numberofrowsinsection: 1"); 
      return 1; 
     } else { 
      NSLog(@"numberofrowsinsection: %i", [responses count]); 
      return [responses count]; 
     } 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    VideoCell *cell = (VideoCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName]; 
    if (cell == nil) { 
     NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil]; 
     cell = [topLevelItems objectAtIndex:0]; 
    } 

    if (indexPath.section == 0) { 
      cell.lblTitle.text = [data title]; 
      cell.lblDescription.text = [data videoDescription]; 
    } else { 
     if ([responses count] == 0) { 
      if (!hasLoadedResponses) { 
       cell.lblTitle.text = @""; 
       cell.lblDescription.text = @""; 
      } else { 
       //Responses have been loaded 
       cell.accessoryType = UITableViewCellAccessoryNone; 
       cell.selectionStyle = UITableViewCellSelectionStyleNone; 

       cell.lblTitle.text = @"No responses to this video"; 
       cell.lblDescription.text = @"Be the first to respond by selecting the \"Set as Destination\" button above"; 
      } 
     } else { 
      //Display the response information 
      cell.lblTitle.text = [[responses objectAtIndex:indexPath.row] valueForKey:@"title"]; 
      cell.lblDescription.text = [[responses objectAtIndex:indexPath.row] valueForKey:@"description"]; 
     } 
    } 

    return cell; 
} 
+0

我不知道在向表中添加行的同時是否在響應中添加了新字段? – 2012-01-29 19:50:18

+0

當視圖加載時,我只在第1部分有1行表示加載,那麼一旦數據被加載,當行被插入到表中時 – dbslone 2012-01-29 20:25:35

+0

在插入新行時,[響應計數]是什麼? – 2012-01-29 22:37:47

回答

1

你的數據源和行數是不同步的。插入該行時,必須同時增加該部分中的行數。在這種情況下,您將不得不增加您在numberOfRowsInSection方法中使用的數組responses的數量。

+0

我很困惑你究竟是什麼意思*「插入行時,必須同時增加該段中的行數。」*是否有辦法手動增加行數? – 2016-03-25 22:28:34

+0

有一個方法'insertRowsAtIndexPaths',它將「插入行」,但你也必須增加'numberOfRowsInSection'的返回值來匹配新的數字。 – Mundi 2016-03-26 07:24:57

相關問題