2012-03-27 105 views
0

當我試圖從表格視圖中刪除一行時,我遇到了tableView崩潰問題。該方法被正確調用,因爲正確地在數據庫中進行了更改。這是我收到的錯誤是:從表格視圖中刪除一行

無效更新:在第0

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

    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    NSManagedObjectContext *context = [delegate managedObjectContext]; 


    NSManagedObject *objectToDelete = [fixedArray objectAtIndex:indexPath.row]; 

    [context deleteObject:objectToDelete]; 

    [delegate saveContext]; 

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

    fixedArray = ([self mondayData]); // this is fetching the data for the array that I supply to the table view 

[self.tableView numberOfRowsInSection:[fixedArray count]]; 

    [self.tableView reloadData]; 


} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return [fixedArray count]; 
} 
+0

你可以顯示你的代碼爲'tableView:numberOfRowsInSection:'? – jonkroll 2012-03-27 18:20:32

+0

我已更新原始帖子以顯示section方法中的行數,並顯示嘗試在編輯後返回正確數量的行,但不幸的是得到相同的結果。 – dmeads89 2012-03-27 21:13:29

回答

0

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法可能不會返回行的相應數量的行數無效。它應該返回以前的行數減去剛刪除的行數。

此外,如果該部分中沒有更多行,則必須從UITableView中刪除該部分。

+0

我已更新原始帖子以顯示節方法中的行數,並顯示嘗試在編輯後返回正確數量的行,但不幸的是得到相同的結果。 – dmeads89 2012-03-27 21:14:59

0

您需要包括

[tableView beginUpdates]; 

[tableView endUpdates]; 

在你的代碼。這兩種方法可以防止UITableView在刪除或插入行時崩潰。

beginUpdates放在你刪除tableView的行之前,然後endUpdates一旦你完成了。

這是documentation

+0

仍是同樣的問題。我認爲我的實施可能有一些問題,你的回答是正確的。謝謝。 – dmeads89 2012-03-27 23:11:23

0
[tableView beginUpdates]; 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 


     NSManagedObject *object = [fixedArray objectAtIndex:indexPath.row]; 

     [object setValue:@"No" forKey:@"selected"]; // this works and persists into the database 

     [delegate saveContext]; 

     fixedArray = ([self mondayData]); 

     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES]; 

    } 


    [tableView endUpdates]; 

    [self.tableView reloadData]; 

這對我來說是最後的工作。