2010-04-06 80 views
1

我是iPhone開發新手。我正在開發一個應用程序,它從數據庫中提取一個值並將其顯示在表格視圖中。我想逐個刪除表格行,但行不刪除,應用程序崩潰。iPhone:刪除UITableView行時崩潰

這裏是我的行刪除代碼:

- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
            forRowAtIndexPath:(NSIndexPath *)indexPath 
{  
if(editingStyle == UITableViewCellEditingStyleDelete) { 
    SanjeevKapoorAppDelegate *appDelegate =(SanjeevKapoorAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    list *animal =[appDelegate.list1 objectAtIndex:indexPath.row]; 
    [appDelegate deleteCoffee];  
    [self.tableView reloadData]; 
    //Delete the object from the table. 
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 

我也有這個動作刪除所有行:

-(IBAction)deletebtn:(id)sender{ 
    SanjeevKapoorAppDelegate *appDelegate =(SanjeevKapoorAppDelegate *)[[UIApplication sharedApplication] delegate]; 
     [appDelegate deleteCoffee]; 
     [self.tableView reloadData]; 
} 

如何正確執行刪除一個的UITableView?

回答

0

你需要改變:

[self.tableView reloadData]; 
//Delete the object from the table. 
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

只是:

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

你崩潰,因爲你改變你的數據模型刪除的行,然後重新加載表,使得行不再存在,然後告訴表刪除不存在的行。

您不必在此實例中調用重裝數據,因爲該表已經以可視方式自行更改。當數據模型中的更改強制更改表格時,只有在表格本身更改數據模型時才調用reload

0

嘗試包裹將呼叫轉接至beginUpdates deleteRows和endUpdates:

[self.tableView beginUpdates]; 
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[appDelegate deleteCoffee];  
[self.tableView endUpdates]; 

我認爲deleteCoffee影響表的填充方式。這必須在更新包裝之間進行。

或者你可以使用reloadData沒有其他電話:

[appDelegate deleteCoffee];  
[self.tableView reloadData];