2013-10-11 51 views
1

我試圖找到一個代碼示例,顯示如何處理tableView中的移動/重排細胞。使用moveRowAtIndexPath

對於去除核心數據我使用如下

- (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 

在相同的風格?

+0

你在FetchRequest上用什麼sortDescription來填充你的表?您需要對數據進行更改,這將反映行的重新排序。 – fguchelaar

回答

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]; 
} 
+0

不可見@interface for'NSArray'聲明選擇器'removeObject' –

+0

使用NSMutableArray。將您的empArray更改爲NSMutableArray而不是NSArray – karthika

+0

由於未捕獲的異常'NSInvalidArgumentException',原因:' - [_ PFArray removeObject:]:無法識別的選擇器發送到實例0x8e2eda0' –