2013-10-15 133 views
0

我需要更新核心數據中所需單元格的值。我有一個對我有價值的單元(ID)。我需要找到價值並取代。我知道如何改變數組的元素。以下顯示了我是如何做到的。但是我必須在MyBase中更改字段(ID)。我需要更改的值將等於fromIndexPath.row。我需要更改toIndexPath.row的值。更新值核心數據

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath 
                  *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
if (fromIndexPath.section == toIndexPath.section) { 

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

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

    MyBase *employee = [empArray objectAtIndex:fromIndexPath.row]; 

     [empArray removeObjectAtIndex:fromIndexPath.row]; 
     [empArray insertObject:objectToMove atIndex:toIndexPath.row]; 
} 

}

回答

0

用於更新核心數據,你可以試試這個:

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@:@"Employee"  
              inManagedObjectContext:self.managedObjectContext]; 
[request setEntity:entity]; 

NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"empId = %d", 12345]]; 
[request setPredicate:pred]; 

NSArray *empArray=[self.managedObjectContext executeFetchRequest:request error:nil]; 
[request release]; 

if ([empArray count] > 0){ 
    Employee *employee = [empArray objectAtIndex:fromIndexPath.row];; 

    employee.empSalary=[NSNumber numberWithInt:45000]; 

    [email protected]"John"; 
    [email protected]"Analysist"; 
    [email protected]"4 Years"; 

    [self.managedObjectContext save:nil];