Reputation: 1437
I'm fairly new to programming with xCode. I try to understand the basics by reading books/tutorials and so on. But sometimes even those can't help. And even thought I always see a challenge in finding out where the error is or what to do, I'm baffled about this one.
I created a normal UITableView. I want the User to enter information in three different UITextFields which are all saved to NSUserDefaults. When doing so, it saves them correctly and I can load them in a normal label.
But I want it to get displayed in one Cell (along with the User's Name that should be saved permanently, so he/she doesnt have to type it over and over again). So when the user enters the data, it will get saved in NSUserDefaults and whenever he switches to the TableView it will get displayed in the first row altogether.
(For example) I tried Cameron Spickerts answer Loading data in NSUserDefaults (changed it to my needs ofc). But I can't seem to get it to work. Maybe because he uses an much older version of xCode? I would be really grateful, if someone could help me with this problem.
Code from .m file:
@synthesize tableViewArray;
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[self.UserData arrayWithObjects:[prefs objectForKey:@"hello"], [prefs objectForKey:@"ha
ello2"], [prefs objectForKey:@"hello3"] nil];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableViewArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@ %@", [self.UserData objectAtIndex:0], [self.UserData objectAtIndex:1], [self.UserData objectAtIndex:2] ];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *message = [NSString stringWithFormat:@"You selected %@",[tableViewArray objectAtIndex:indexPath.row]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message: message delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
}
Upvotes: 1
Views: 3081
Reputation: 2555
Ok, answer to
How can I store data in
self.UserData?
It' really easy. Like this:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:_value forKey:@"yourUserDefaultsEntryName"];
You can read it then
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[self.UserData arrayWithObjects:[prefs objectForKey:@"yourUserDefaultsEntryName"], [prefs objectForKey:@"yourAnotherUserDefaultsEntryName"], nil];
Hope that'll help.
Upvotes: 1
Reputation: 2555
You should implement several UITableViewDelegate
and UITableViewDataSource
methods. For that, make sure you use a subclass of UITableViewController
or your ViewController conforms to these protocols. Like this:
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
For example, you store your data from NSUserDefaults
in self.userData
property, which is a array. You should fill this NSArray
for example in your -viewDidLoad
method.
Then, make sure you have these methods implemented:
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Set up the number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.userData count];
}
Implementation of the next method depends on your iOS version, in iOS5 you will write this:
// Then fill the cells with data
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [self.userData objectAtIndex:indexPath.row];
}
Then follows non-essentional in your case methods of UITableViewDelegate
, you can define them later.
Make sure you have proper bindings in your interface builder or storyboards. Your tableViews's delegate
and dataSource
outlets have to be bound to your ViewController.
Hope it helps. It'll help me to answer exactly to the point, if you show me your code.
Upvotes: 2