2012-08-28 85 views
0

我是iPhone中的新手,我想從我的UITableView中刪除一個單元格,第一次刪除的很好,但第二次它給了我以下錯誤:uiTableView在第二次刪除單元格時崩潰

*** 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 (3) must be equal to the number 
of rows contained in that section before the update (3), plus or minus the number 
of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or 
minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 

這裏是我的表的代碼:

- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
Book_own *temp= (Book_own *)[self.books objectAtIndex:indexPath.row]; 

if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Delete the row from the data source 

    [books removeObjectAtIndex:indexPath.row]; 

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    isDeleted = @"true"; 
    deletedBook_id = temp.bo_id; 

    [self viewDidLoad]; 

} 

else if (editingStyle == UITableViewCellEditingStyleInsert) { 
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
} 

} 


- (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. 

NSDictionary *dictionary = [listOfItems objectAtIndex:section]; 
NSArray *array = [dictionary objectForKey:@"myBooks"]; 
return [array count]; 
} 

在viewDidLoad中我寫了下面的代碼:

NSDictionary *mbooks = [NSDictionary dictionaryWithObject:books forKey:@"myBooks"]; 

NSDictionary *mgroups = [NSDictionary dictionaryWithObject:filteredParts forKey:@"myBooks"]; 
listOfItems = [[NSMutableArray alloc] init]; 

[listOfItems addObject:mbooks]; 
[listOfItems addObject:mgroups]; 

誰能告訴我如何解決這個問題? 在此先感謝。

+3

所以..你在你的表格視圖中有3行,你刪除了一行,而你**仍然**在你的表格視圖中有3行...這就是投訴 – nielsbot

回答

1

如果我正確讀取您的代碼,您的數據源是listOfItems。您應該從表格數據源中刪除該行。一般的規則是,當您刪除或添加項目到UITableView時,您必須更新數據源。

[listOfItemsremoveObjectAtIndex:indexPath.row]; 
+0

我試過了,但它仍然給我異常:( – user1553381

0

首先,不要調用[self viewDidLoad];不應該手動調用此方法。

我認爲你不是調用你的表視圖的更新方法。這可能會解決您的問題:

[tableView beginUpdates]; 
[books removeObjectAtIndex:indexPath.row]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[tableView endUpdates]; 

編輯:您還需要更仔細地查看您的代碼。您正在從數據源數組中直接從indexPath行刪除記錄,考慮到您的tableView有兩個部分,這可能會有問題。

0

錯誤提示刪除後應該有少一行。它失敗是因爲數據源代碼在報告模型方面不一致。

看看你如何回答numberOfRowsInSection。正確地說,我認爲,首先挑出節索引代表的數組,然後回答該數組的計數。

相同的邏輯必須適用於刪除。您從中刪除的數組需要是由indexPath.section指示的數組。試想一下:

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

    // the array you manipulate here must be the same array you use to count 
    // number of rows, and the same you use to render rowAtIndexPath 

    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
    NSArray *array = [dictionary objectForKey:@"myBooks"]; 

    // in order to be edited, it must be mutable 
    // you can do that on the fly here, or set it up as mutable 
    // let's make it temporarily mutable here: 
    NSMutableArray *mutableCopy = [array mutableCopy]; 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [mutableCopy removeObjectAtIndex:indexPath.row]; 

     // if it's setup mutable, you won't need to replace the immutable copy in the dictionary. 
     // but we just made a copy, so we have to replace the original 
     [listOfItems replaceObjectAtIndex:indexPath.section withObject:[NSArray arrayWithArray:mutableCopy]]; 

     // and so on 
+0

listOfItems是一個NSMutableArray而不是NSMutableDictionary – user1553381

+0

沒錯,編輯。我希望你會在這裏看到的關鍵想法是acces s到你的模型必須是所有數據源方法中indexPath的相同函數。 – danh

+0

我試過了,它給了我以下例外: - [__ NSArrayI objectForKey:]:無法識別的選擇器發送到實例0xebe20c0 2012-08-28 21:24:02。782 eBook_App [460:f803] ***由於未捕獲的異常'NSInvalidArgumentException',原因:' - [__ NSArrayI objectForKey:]:無法識別的選擇器發送到實例 – user1553381

1

正在發生的事情是,你要麼不刪除項目或只刪除一個項目時,你允許multible刪除。您可以通過檢查它是否真的刪除如下調整或重新加載數據,如果沒有:

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     BOOL success = [self removeFile:indexPath]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     if (!success) [tableView reloadData]; 
    } 
} 

這種方法然後刪除數據源項目(這是從我自己的項目,以便它們的名稱應該進行調整:

-(BOOL) removeFile:(NSIndexPath *)indexPath{ 
    // Removes from the datasource and the filesystem 
    NSURL *fileURL = [self.dataSource objectAtIndex:indexPath.row]; 
    NSError *error; 
    BOOL success = [[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error]; 
    if (success) { 
     [self.dataSource removeObjectAtIndex:indexPath.row]; 
    } else { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
     [alert show]; 
     [alert release]; 
     [self.dataSource removeObjectAtIndex:indexPath.row]; 
    } 
    return success; 
} 
+0

我終止了應用程序但它仍然給我這個例外:( – user1553381

+0

使用NSLog在刪除一行後打印出每個部分的項目數量。另外,您是否允許多次刪除? –