- UITableView - Loading Data from NSUserDefaults
- @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];
- @interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- // 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];
- }
- // 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];
- }
- NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
- [prefs setObject:_value forKey:@"yourUserDefaultsEntryName"];
- NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
- [self.UserData arrayWithObjects:[prefs objectForKey:@"yourUserDefaultsEntryName"], [prefs objectForKey:@"yourAnotherUserDefaultsEntryName"], nil];