0

I need to update the value of the desired cell in my core data. I have a cell (ID) with a known value to me. I need to find the value and replace. I know how to change the elements of the array. The following shows how I do it. But I have to change the field (ID) in MyBase. value that I need to change will be equal to fromIndexPath.row. The value on which I need to change 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];
}

}

4

1 回答 1

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];

    employee.empName=@"John";
    employee.empDesignation=@"Analysist";
    employee.empExp=@"4 Years";

    [self.managedObjectContext save:nil];
于 2013-10-15T19:56:33.040 回答