Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone:删除UITableView行时崩溃_Iphone_Objective C_Uitableview - Fatal编程技术网

iPhone:删除UITableView行时崩溃

iPhone:删除UITableView行时崩溃,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我是iPhone开发的新手。我正在开发一个应用程序,它从数据库中获取一个值并将其显示在表视图中。我想一行一行地删除表中的行,但这些行不会删除,应用程序会崩溃 这是我的行删除代码: - (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSInde

我是iPhone开发的新手。我正在开发一个应用程序,它从数据库中获取一个值并将其显示在表视图中。我想一行一行地删除表中的行,但这些行不会删除,应用程序会崩溃

这是我的行删除代码:

- (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中正确实现删除?

您需要更改:

[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];
您正在崩溃,因为您正在更改数据模型以删除该行,然后重新加载该表,使该行不再存在,然后通知该表删除不存在的行


在这个实例中,您不必调用reloaddata,因为表已经在视觉上改变了自己。仅当数据模型中的更改强制表中的更改时,而不是当表本身更改数据模型时,才调用reload

尝试在BeginUpdate和EndUpdate中包装对deleteRows的调用:

[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[appDelegate deleteCoffee];      
[self.tableView endUpdates];
我假设删除咖啡会影响表格的填充方式。这必须介于更新包装器之间

或者,您可以在不进行其他调用的情况下使用重载数据:

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