2011-03-28 50 views
15

每當我有我的UITableView中的數據,我開始刪除,它工作正常。但是,當我到達表中的最後一個對象並刪除它時,它會崩潰。NSInternalInconsistencyException(無效的行數)

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

以下是我正在做的東西編輯:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     if ([myData count] >= 1) { 
      [tableView beginUpdates]; 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [myData removeObjectAtIndex:[indexPath row]]; 

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsDirectory = [paths objectAtIndex:0]; 
      NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"]; 
      [myData writeToFile:somepath atomically:YES]; 
      [table reloadData]; 
      if ([myData count] == 0) { 
       [tableView endUpdates]; 
       [tableView reloadData]; 
      } 
      else { 
      [tableView endUpdates]; 
      } 
     } 
    } 
} 

而且也是這樣:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    if ([myData count] != 0) { 
     return [myData count]; 
    } 
    else { 
     return 1; 
    } 
} 

我回到1的原因是因爲我正在做在cellForRowAtIndexPath中表示「沒有數據保存」的單元格。這就是我的意思:

-(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] autorelease]; 
    }  
    if ([cityData count] != 0) { 
     //normal setup removed for clarity 
    } 
    else { 
     cell.textLabel.text = @"No saved data!"; 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14]; 
    cell.textLabel.textAlignment = UITextAlignmentCenter; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     cell.tag = 1; 
    return cell; 
    } 
} 

那麼,我在做錯誤的編輯代碼中得到這個錯誤?謝謝!

回答

28

如果刪除表中的最後一行,則UITableView代碼預計剩餘0行。它會調用您的方法來確定剩餘的數量。由於您有一個「無數據」單元格,它返回1而不是0.因此,當您刪除表格中的最後一行時,請嘗試調用-insertRowsAtIndexPaths:withRowAnimation:來插入「無數據」行。此外,您不應該在此方法的任何地方撥打-reloadData-endUpdates將負責重新加載受影響的行。試試這個:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     if ([myData count] >= 1) { 
      [tableView beginUpdates]; 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [myData removeObjectAtIndex:[indexPath row]]; 

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsDirectory = [paths objectAtIndex:0]; 
      NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"]; 
      [myData writeToFile:somepath atomically:YES]; 

      if ([myData count] == 0) { 
       [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      } 
      [tableView endUpdates]; 
     } 
    } 
} 
+1

完美,謝謝! – 2011-03-28 05:00:36

+3

你也可以做的只是調用tableView deleteRowsAtIndexPaths,如果[數據計數]> 0,然後DO調用reloadData。 這樣你就不會刪除最後一個單元,如果它將被用於「無數據」單元,並且重載會確保委託在桌面視圖中加載正確的信息。 – EeKay 2012-08-30 10:09:02

+0

謝謝,這對我也有幫助! – 2013-06-02 18:33:15

1

首先從myData中刪除,然後從tableview中刪除。

-(void)tableView:(UITableView *)tableView commitEditingStyle: 
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //somehting... 
     [myData removeObjectAtIndex:[indexPath row]]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     //somehting... 
    } 
} 

}

1

的方法tableView:numberOfRowsInSection必須始終返回行的確切數字:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [myData count]; 
} 

其刪除最後一行之後,您可能需要刪除整段。只需在beginUpdatesendUpdated區塊內撥打deleteSections:withRowAnimation:;

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [myData removeObjectAtIndex:[indexPath row]]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *somepath = [documentsDirectory stringByAppendingPathComponent:@"something.plist"]; 
     [myData writeToFile:somepath atomically:YES]; 
     if ([myData count] == 0) { 
      // NEW! DELETE SECTION IF NO MORE ROWS! 
      [tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationFade]; 
     } 
     [tableView endUpdates]; 
    } 
} 
相關問題