Quote:
Originally Posted by DrJones
Hi, im currently developing an app where the user can take notes. I use UITableViewCells to set up an titleCell and a noteCell. That works great. I use NSUserDefaults to save the notes, and that works fine. The problem occurs when i return to the note view controller. The text is moved around and i can't edit it. If i go back and return again, the text is gone.
I use this code to save:
PHP Code:
- (IBAction)saveNote:(id)sender
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:titleTextField.text forKey:@"titleCellName"];
[prefs setObject:noteTextView.text forKey:@"noteCellText"];
[prefs synchronize];
[self dismissModalViewControllerAnimated:YES];
}
when i retrieve the loaded data, i do as following:
PHP Code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *titleText = [prefs objectForKey:@"titleCellName"];
NSString *noteText = [prefs objectForKey:@"noteCellText"];
titleCell.textLabel.text = titleText;
noteCell.textLabel.text = noteText;
}
can anyone help me figure out what is wrong? 
|
You've got the wrong idea on how to use table views.
You should have a data source that uses an ordered list of information. Frequently, one or more NSArray objects does the job. You implement a cellForRowAtIndexPath method that takes data from your ordered list at the specified row (and possibly section) and uses it to populate a cell when the system asks you for that data.
As the user scrolls your list, he/she will expose new cells, and the system will call your cellForRowAtIndexPath method for those new rows/sections. The user might scroll a row off the screen, then scroll it back. You use an ordered list (like an NSArray) to keep the data and create cells to display that data as needed.
When you read your data back from disk, your read it into your ordered list data structure (again, usually one or more NSArrays) and then call the table view's reloadData method. The table view then asks you to create cells for some number of indexPath values.
Your viewDidLoad method is simply plugging data into fixed cells (which probably don't exist yet.) That's not how it works.