PDA

View Full Version : populating the plist




StefanDevil
Aug 21, 2010, 03:20 PM
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:


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





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.:)



dejo
Aug 21, 2010, 03:38 PM
We'll need to see your complete code for didSelectRowAtIndexPath:

StefanDevil
Aug 21, 2010, 03:53 PM
here it is:


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

dejo
Aug 21, 2010, 04:15 PM
when user selected a cell, i will be display at the other controller.
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.

for controller 2 i place the code for loading the plist at viewWillAppear:
but it doesnt seems to work
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?

StefanDevil
Aug 22, 2010, 01:15 AM
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.


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.


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?

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

dejo
Aug 22, 2010, 01:31 AM
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.
All calls to didSelectRowAtIndexPath: are writing to the same Data.plist file. Why would you expect it not to overwrite?

StefanDevil
Aug 22, 2010, 06:53 AM
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:)

dejo
Aug 22, 2010, 11:02 AM
how can i solve this problem?
I would start by following chown33's advice:

Post your code. Describing code does nothing. We can't debug descriptions. Even if we could, the translation from description to code could be incorrect.

StefanDevil
Aug 22, 2010, 01:28 PM
hey i manage to solve it :>
thks ya:cool: