Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 2.34 KB  |  hits: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. UITableView - Loading Data from NSUserDefaults
  2. @synthesize tableViewArray;
  3.  
  4.  
  5.  
  6.  - (void)viewDidLoad
  7. {
  8.    [super viewDidLoad];
  9.  
  10. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  11. [self.UserData arrayWithObjects:[prefs objectForKey:@"hello"], [prefs objectForKey:@"ha
  12. ello2"], [prefs objectForKey:@"hello3"] nil];
  13.  
  14.  
  15.  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  16. {
  17.  
  18. return [tableViewArray count];
  19.    }
  20.  
  21.   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  22.   {
  23.    static NSString *CellIdentifier = @"Cell";
  24. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  25. cell.textLabel.text = [NSString stringWithFormat:@"%@ %@ %@", [self.UserData objectAtIndex:0], [self.UserData objectAtIndex:1], [self.UserData objectAtIndex:2] ];
  26.  
  27. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  28.   {
  29.  
  30.     NSString *message = [NSString stringWithFormat:@"You selected %@",[tableViewArray objectAtIndex:indexPath.row]];
  31.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
  32.                                                     message: message delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
  33.     [alert show];
  34.        
  35. @interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
  36.        
  37. // Customize the number of sections in the table view.
  38. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  39. {
  40.     return 1;
  41. }
  42.  
  43. // Set up the number of rows
  44. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  45. {
  46.     return [self.userData count];
  47. }
  48.        
  49. // Then fill the cells with data
  50. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  51. {
  52.     static NSString *CellIdentifier = @"Cell";
  53.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  54.     cell.textLabel.text = [self.userData objectAtIndex:indexPath.row];
  55. }
  56.        
  57. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  58. [prefs setObject:_value forKey:@"yourUserDefaultsEntryName"];
  59.        
  60. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  61. [self.UserData arrayWithObjects:[prefs objectForKey:@"yourUserDefaultsEntryName"], [prefs objectForKey:@"yourAnotherUserDefaultsEntryName"], nil];