2010-04-06 83 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];