Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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_Cocoa Touch_Uitableview - Fatal编程技术网

Iphone UITableView在尝试删除单元格时出现问题

Iphone UITableView在尝试删除单元格时出现问题,iphone,objective-c,cocoa-touch,uitableview,Iphone,Objective C,Cocoa Touch,Uitableview,我有一个UITableViewController,当我尝试滑动删除单元格并按delete键时,它会将单元格切换到编辑模式,并在左侧显示红色箭头。但是数据已经被删除了。因为当我重新启动应用程序时,它不见了。我的代码中有可疑的东西吗 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)setEditing:(BOO

我有一个UITableViewController,当我尝试滑动删除单元格并按delete键时,它会将单元格切换到编辑模式,并在左侧显示红色箭头。但是数据已经被删除了。因为当我重新启动应用程序时,它不见了。我的代码中有可疑的东西吗

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
    [self.tableView setEditing: !self.tableView.editing animated:YES];

    if (self.tableView.editing)
        [self.navigationItem.leftBarButtonItem setTitle:@"Done"];
    else
        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
     if (editingStyle == UITableViewCellEditingStyleDelete) 
     {
         PFObject *routine= [self.routineArray objectAtIndex:indexPath.row];
         [routine deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
             if (!error) {
                 [self.tableView reloadData];
             } else {
                 // There was an error saving the gameScore.
             }
         }];   
     }
 }

您必须从数据源
self.routineArray
中删除该项,然后使用
-deleteRowsAtIndexPaths:withRowAnimation:
将其从表视图中删除,然后将其从核心数据存储中删除。顺序很重要,因为如果它仍然存在于数据源中,则无法将其从tableView中删除,或者由于tableView与其数据源中的项数不同步而引发异常

在本例中,它首先从数据存储中删除,然后从数据源中删除,然后从tableView中删除,这应该也可以

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        PFObject *routine= [self.routineArray objectAtIndex:indexPath.row];
        [routine deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error) {
                [self.routineArray removeObjectAtIndex:indexPath.row];
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            } else {
                // There was an error saving the gameScore.
            }
        }];   
    }
}

您必须从数据源
self.routineArray
中删除该项,然后使用
-deleteRowsAtIndexPaths:withRowAnimation:
将其从表视图中删除,然后将其从核心数据存储中删除。顺序很重要,因为如果它仍然存在于数据源中,则无法将其从tableView中删除,或者由于tableView与其数据源中的项数不同步而引发异常

在本例中,它首先从数据存储中删除,然后从数据源中删除,然后从tableView中删除,这应该也可以

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        PFObject *routine= [self.routineArray objectAtIndex:indexPath.row];
        [routine deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error) {
                [self.routineArray removeObjectAtIndex:indexPath.row];
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            } else {
                // There was an error saving the gameScore.
            }
        }];   
    }
}

您好,这已成功,但动画仍在设置中。当我刷卡然后按delete键时,它会在左边显示减号,最后删除单元格。这就是我所说的顺序很重要。您返回的内容是什么?tableView:numberOfRowsInSection:?必须确保该数据源方法中返回的行数与表视图中的行数相同。如果tableView在任何时候调用此方法,并且号码不同,它将抛出
nsinternalinconsistenceexception
。您是否正在使用
NSFetchedResultsController
管理表视图?尝试从
self.routineArray
中删除对象,然后首先从tableView中删除该行,并确保您没有运行
[tableView reloadData]
我再也不会遇到崩溃,但动画已经搞砸了。如果你能看一看的话,我在这里用一个视频提出了一个新问题。谢谢您好,这已成功,但动画仍在设置中。当我刷卡然后按delete键时,它会在左边显示减号,最后删除单元格。这就是我所说的顺序很重要。您返回的内容是什么?tableView:numberOfRowsInSection:?必须确保该数据源方法中返回的行数与表视图中的行数相同。如果tableView在任何时候调用此方法,并且号码不同,它将抛出
nsinternalinconsistenceexception
。您是否正在使用
NSFetchedResultsController
管理表视图?尝试从
self.routineArray
中删除对象,然后首先从tableView中删除该行,并确保您没有运行
[tableView reloadData]
我再也不会遇到崩溃,但动画已经搞砸了。如果你能看一看的话,我在这里用一个视频提出了一个新问题。谢谢