使用moveRowAtIndexPath

时间:2013-10-11 09:53:59

标签: iphone objective-c uitableview

我正在尝试找到一个代码示例,演示如何在tableView中处理移动/重新排列单元格。

为了删除Core数据,我使用下面的代码

- (void)tableView:(UITableView *)aTableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete)

{

    NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription* entityDescription = [NSEntityDescription entityForName:@"MyBase"
                                                         inManagedObjectContext:self.objectContext];
    [fetchRequest setEntity:entityDescription];


    NSArray *empArray=[self.objectContext executeFetchRequest:fetchRequest error:nil];

    if ([empArray count] > 0){
        Moneybox *employee = [empArray objectAtIndex:indexPath.row];
        [self.objectContext deleteObject:employee];
        [self.objectContext save:nil];

    }

    [self buscarContactoExistente];
    [myTable reloadData];

}

}

如何制作

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 

以同样的风格?

1 个答案:

答案 0 :(得分:1)

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath 
      toIndexPath:(NSIndexPath *)toIndexPath 
{
    Moneybox *employee = [empArray objectAtIndex:fromIndexPath.row];
    [empArray removeObject: employee];
    [empArray insertObject:employee atIndex:toIndexPath.row];
}