2015-07-22 52 views
0

我有一个项目,我一直在努力。代码给我的问题昨天工作,我没有做任何改变的代码的部分,因为,当我试图运行它今天它无法正常工作。Xcode commitEditingStyle UITableViewCellEditingStyleDelete不工作

问题是,当我滑过单元格以删除它时,未检测到它,并且commitEditingStyle方法从不运行。

我也做了另一个项目只是为了测试commitEditingStyle方法,它工作正常。我还清理了项目(产品 - >清洁)并重置了iOS模拟器。

我还能试试吗?

这是我的代码,不用担心NSUserDefaults,这是为了在删除单元格后保存更改。

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; } 
    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [self.taskObjects removeObjectAtIndex:indexPath.row]; 

     NSMutableArray *newTaskObjectsData = [[NSMutableArray alloc] init]; 
     for(Model *task in self.taskObjects) 
     { 
      [newTaskObjectsData addObject:[self taskObjectAsPropertyList:task]]; 
     } 

     [[NSUserDefaults standardUserDefaults] setObject:newTaskObjectsData forKey:USER_KEY]; 
     [[NSUserDefaults standardUserDefaults] synchronize]; 

     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

回答

0

我发现了什么是错误的!我在viewController中有另一个方法,我不知道在那里。

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleNone; 
} 

通过将返回值设置为UITableViewCellEditingStyleDelete来修复它。

0

我希望它帮你

if (editingStyle == UITableViewCellEditingStyleDelete) 
{ 
      [self.taskObjects removeObjectAtIndex:indexPath.row]; 

     NSMutableArray *newTaskObjectsData = [[NSMutableArray alloc] init]; 
     for(Model *task in self.taskObjects) 
     { 
      [newTaskObjectsData addObject:[self taskObjectAsPropertyList:task]]; 
     } 

     [[NSUserDefaults standardUserDefaults] setObject:newTaskObjectsData forKey:USER_KEY]; 
     [[NSUserDefaults standardUserDefaults] synchronize]; 

     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; // please set automatic animation 

    } 
// here you can reload your tableview 
[self.tasksTableView reloadData]; 
+1

它仍然没有工作,问题是当用户在单元格上滑动时,方法commitEditingStyle永远不会被调用。 – Ben