Edit NSTableView - NSArrayController

Discussion in 'Mac Programming' started by kikko088, Jan 17, 2013.

  1. kikko088, Jan 17, 2013
    Last edited: Jan 17, 2013

    macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #1
    Hi at all I have some problem tryng to editing value inside nstableview, I binding the table with an nsarraycontroller, for add and editing a value I use this methods:

    Code:
    - (IBAction) addNewMateria:(id)sender{
            
            isNew = YES;
            
            Materia *newMat = [[Materia alloc] init];
            
            [self.arrayMaterieController addObject:newMat];
    }
    - (void) editingDidEnd:(NSNotification *)notification {
            
                 NSTableView *table = [notification object];
                    
                 Materia *newMat = [[Materia alloc] init];
                    
                 if([table selectedRow] != -1) {
                     NSArray *selectedObjects = [self.arrayMaterieController selectedObjects];
                     newMat = [selectedObjects objectAtIndex:0];
                 }
            
                 if (isNew == YES) {
                    
                     if (newMat.idMateria != nil && newMat.titoloMateria!= nil && newMat.iconaMateria!= nil){
                      
                         NSString *strQuery = [NSString stringWithFormat:@"INSERT INTO Materie (IDMateria, TitoloMateria, IconaMateria) VALUES ('%@', '%@', '%@')",
                                                  newMat.idMateria,newMat.titoloMateria,newMat.iconaMateria];
                         [dataLoad  update:strQuery];
                        
                         isNew = NO;
                     }
                 }
                 else {
                     NSString *strQuery = [NSString stringWithFormat:@"UPDATE Materie SET IDMateria='%@', TitoloMateria='%@' ,IconaMateria='%@' WHERE IDMateria='%@'",
                                           newMat.idMateria,newMat.titoloMateria,newMat.iconaMateria,self.materiaOld.idMateria];
                     [dataLoad  update:strQuery];
           }
    }
    
    - (void) controlTextDidBeginEditing:(NSNotification *)aNotification {
            
            if ([[aNotification object] isKindOfClass:[NSTableView class]]) {
                
                NSLog(@"Tabella strt editing");
                
                NSTableView *aTableView = [aNotification object];
                
                if (aTableView == tabellaMateria) {
                    self.materiaOld = [self.arrayMaterie objectAtIndex:[aTableView selectedRow]];
                }
            }
    }
    
    my problem is that when I edit a value the object oldMaterie change with new object, so I can't update the value on my database because I can't store the old id of object, where is the mistake?
     

Share This Page