title: how to save the state in userdefaults of accessory checkmark-iphone
body: <p>I am working on an application having <code>UItableView</code>. In the rows of the table i am able to put checkmarks.Now how to save the state of checkmarks so that even if user close the application
  the state should be shaved and in the next launch of application checkmark should be shown.
  i have followed the tutorials on <code>NSUSerDefaults</code> but Pulling my hairs where to put the codes of saving and retrieving.I have tried but every time errors are stuffing me and not able to fix.
  My Code:  </p>
  
  <p>MY.h file</p>
  
  <pre><code>**@protocol LocationSelectionViewControllerDelegate &lt;NSObject&gt;
  
  @required
  - (void)rowSelected:(NSString *)selectedValue selectedIndex:(NSInteger)index;
  @end**
  
  @interface LocationSelection : UIViewController &lt;UITableViewDelegate,UITableViewDataSource&gt;{
  
  UITableView *table;
  ***NSInteger       selectedIndex;***
  NSMutableArray *menuList;
  ***id &lt;LocationSelectionViewControllerDelegate&gt;    delegate;***
  
  }
  @property (nonatomic,retain) NSMutableArray *menuList;  
  @property (nonatomic,retain) IBOutlet UITableView *table;  
  ***@property (nonatomic, assign) id &lt;LocationSelectionViewControllerDelegate&gt; delegate;**  
  **@property NSInteger selectedIndex;***  
  @end  
  </code></pre>
  
  <p>my .m file:  </p>
  
  <pre><code>@implementation LocationSelection  
  ***@synthesize menuList, table,selectedIndex,delegate;***
  
  - (void)viewDidLoad
  {
      menuList = [[NSMutableArray alloc] initWithObjects:
                  [NSArray arrayWithObjects:@"LOCATION1", nil],
                  [NSArray arrayWithObjects:@"LOCATION2", nil],
                  [NSArray arrayWithObjects:@"LOCATION3", nil],
                  nil];
  
      self.title = @"Location Selection";
  
      [table reloadData];
      [super viewDidLoad];
  }
  
  //MY CELLFORROWATINDEXPATH  
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
  {
      static NSString *CellIdentifier = @"CellIdentifier";
  
      UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
      }
      cell.highlighted = NO;
  
      ***NSArray * rowArray = [menuList objectAtIndex:indexPath.row];***
  
      UILabel * nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15, 8, 200, 20)] autorelease];
      nameLabel.text = [NSString stringWithFormat:@"%@", [rowArray objectAtIndex:0]];
      [cell.contentView addSubview:nameLabel];
  
      ***cell.accessoryType = (rowArray == selectedIndex &amp;&amp; selectedIndex &gt; -1 &amp;&amp; selectedIndex &lt; [menuList count]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
      return cell;
      ***
  }
  
  //MY DIDSELECTROWATINDEXH  
  
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
      int newRow = [indexPath row];
  
      if (newRow != selectedIndex)
      {
          UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
          newCell.accessoryType = UITableViewCellAccessoryCheckmark;
  
          if (selectedIndex &gt; - 1 &amp;&amp; selectedIndex &lt; [menuList count])
          {
              NSUInteger newIndex[] = {0, selectedIndex};
              NSIndexPath *lastIndexPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
              UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
              oldCell.accessoryType = UITableViewCellAccessoryNone;
          }       
  
          selectedIndex = newRow;  
          NSString *selectedValue=[menuList objectAtIndex:selectedIndex];
          [self.delegate rowSelected:selectedValue selectedIndex:selectedIndex];
      }
  }
  </code></pre>