populating the plist

Discussion in 'iOS Programming' started by StefanDevil, Aug 21, 2010.

  1. macrumors member

    Joined:
    Jul 31, 2010
    #1
    hi there,

    looking for some help.. i have 2 controllers, 1 contains a custom cell table. when user selected a cell, i will be display at the other controller. so far my approach is when at controller 1, if cell selected, save to plist, than at controller 2, load the plist.

    my saving and loading code:

    Code:
    NSMutableArray *selectedData;
    selectedData = [[NSMutableArray arrayWithCapacity:10]retain];  //10 for 10 cells
    
    NSArray *keys = [NSArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five",@"six",nil];
     
     [selectedData addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
     [NSString stringWithFormat:@"%@",@"test1"], 
     [NSString stringWithFormat:@"%@",@"test2"], 
     [NSString stringWithFormat:@"%@",@"test3"], 
     [NSString stringWithFormat:@"%@",@"test4"], 
     [NSString stringWithFormat:@"%@",@"test5"], 
     [NSString stringWithFormat:@"%@",@"test6"],nil]forKeys:keys]];
     
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"];
    [path retain];
    [selectedData writeToFile:path atomically:YES];
    
    
    Code:
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"];
    self.data = [NSArray arrayWithContentsOfFile:path];	
    
    
    now the problem:
    i implement the saving to plist code at didSelectRowAtIndexPath: of controller 1, so that when user select a row it will save to plist.
    but the problem is it only save at the 1st slot/cell. it will keep on over writing. and it will only get display at controller 2 when i exit the app, it doesnt display immediately.

    for controller 2 i place the code for loading the plist at viewWillAppear:
    but it doesnt seems to work

    i am missing something..

    thx in adv.:)
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    We'll need to see your complete code for didSelectRowAtIndexPath:
     
  3. thread starter macrumors member

    Joined:
    Jul 31, 2010
    #3
    here it is:

    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    	NSDictionary *dataItem = [data objectAtIndex:indexPath.row];	
    	NSString *selected;
    	selected = [dataItem objectForKey:@"HI"];	
    	
    	//save to plist
    	NSMutableArray *selectedData;
    selectedData = [[NSMutableArray arrayWithCapacity:10]retain];  //10 for 10 cells
    
    NSArray *keys = [NSArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five",@"six",nil];
     
     [selectedData addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
     [NSString stringWithFormat:@"%@",@"test1"], 
     [NSString stringWithFormat:@"%@",@"test2"], 
     [NSString stringWithFormat:@"%@",@"test3"], 
     [NSString stringWithFormat:@"%@",@"test4"], 
     [NSString stringWithFormat:@"%@",@"test5"], 
     [NSString stringWithFormat:@"%@",@"test6"],nil]forKeys:keys]];
     
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"];
    [path retain];
    [selectedData writeToFile:path atomically:YES];
    
    
    	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
    						  @"Hey"
    					message:selected						 																					   delegate:nil 
    										  cancelButtonTitle:@"cancel" 
    										  otherButtonTitles:nil
    						  ];
    	[alert show];
    	[alert release];	
    
    }
    
    
    thks for your time
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Are you saying you want the selection of the cell to trigger the display of the view for the second controller? If so, your code is not even trying to do that.

    Doesn't seem to work how? Do you have any compile-time errors/ warnings? How about at run-time? Is it crashing? Are you certain viewWillAppear: is even being called? How are you causing the view for controller 2 to appear? What is the relationship between the two controllers? Master-detail?
     
  5. thread starter macrumors member

    Joined:
    Jul 31, 2010
    #5
    no, i don't it to trigger as in "drill in" and display other view.

    put it this way, i would like the selected cell from controller 1 to be save. and than if user can see his selected cell at controller 2.

    no error. contoller 2 only display 1 cell. if viewWillAppear not called, nothing is display.

    one of the problem should be at didSelectRowAtIndexPath:
    everytime i selected a cell and save. it just over write the previous save cell in the plist.

    how can "move down" the plist?

    thks
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    All calls to didSelectRowAtIndexPath: are writing to the same Data.plist file. Why would you expect it not to overwrite?
     
  7. thread starter macrumors member

    Joined:
    Jul 31, 2010
    #7
    hi,

    i manage to solve the overwrite problem.

    now at my controller 2, if i delete all the cell when during simulation, resulting in a empty table, and i go to controller 1 and select a cell, it hangs.

    this problem doesn't occur if controller 2 is not empty.

    in a way my controller 1 cannot save to plist if it is the plist is empty.

    how can i solve this problem?

    thks for the time:)
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    I would start by following chown33's advice:

     
  9. thread starter macrumors member

    Joined:
    Jul 31, 2010

Share This Page