Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

StefanDevil

macrumors member
Original poster
Jul 31, 2010
38
0
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.:)
 

StefanDevil

macrumors member
Original poster
Jul 31, 2010
38
0
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
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors member
Original poster
Jul 31, 2010
38
0
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors member
Original poster
Jul 31, 2010
38
0
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:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.