How to save a checkmark in table view using NSUserDefaults

Discussion in 'iPhone/iPad Programming' started by Schnigges, May 3, 2011.

  1. macrumors newbie

    Joined:
    Feb 22, 2010
    Location:
    Germany
    #1
    Hi,

    how is it possible to save a checkmark in a table view?
    I think the viewWillAppear method is the right place to to so, but i cannot figure it out.
    Would be nice if someone could help me with that.
    Here's the code i have written so far...in the .h file the currently selected cell and its text is saved.

    Code:
    #import "DetailTableViewController.h"
    
    
    @implementation DetailTableViewController
    
    @synthesize savedTime;
    @synthesize savedCell;
    
    - (id)initWithStyle:(UITableViewStyle)style
    {
        self = [super initWithStyle:style];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)dealloc
    {
        [super dealloc];
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        wakeUpTimes = [[NSArray alloc] initWithObjects:@"8:00",@"9:00",@"10:00",nil];
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    }
    
    - (void)viewDidAppear:(BOOL)animated
    {
        ?????
        
        [super viewDidAppear:animated];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    }
    
    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [wakeUpTimes count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        
        // Configuring the cell
        NSString *time = [wakeUpTimes objectAtIndex:indexPath.row];
        cell.textLabel.text = time;
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        return cell;
    }
    
    #pragma mark - Table view delegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // JUST ONE ITEM SHALL BE SELECTABLE
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        NSInteger catIndex = [wakeUpTimes indexOfObject:self.savedTime];
        if (catIndex == indexPath.row) {
            return;
        }
        NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:catIndex inSection:0];
        
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        if (newCell.accessoryType == UITableViewCellAccessoryNone) {
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            self.savedTime = [wakeUpTimes objectAtIndex:indexPath.row];
            self.savedCell.textLabel.text = self.savedTime;
            
            NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
            [userPreferences setObject:self.savedTime forKey:@"savedTime"];
        }
        
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
        if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
            oldCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    
    @end
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    The place to restore the checkmark is cellForRowAtIndexPath. Something like

    Code:
    if (indexPath.row == self.selectedRowIndex)
    // set the checkmark
    else
    // make sure the checkmark isn't visible
     
  3. ulbador, May 3, 2011
    Last edited: May 3, 2011

    macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #3
    Think a little bit about the problem. Basically, when someone checks a row, you have to store that fact as data somewhere. In your case, the logical place is to store it in the object that you use to build the table row.

    The longer answer is this:

    You have to do it when you "build" your table in "cellForRowAtIndexPath". This method gets called however many times based on the numberOfSections/numberOfRows returns when the reloadData method of the table object is executed.

    This line would have to change:

    cell.accessoryType = UITableViewCellAccessoryNone;

    When someone checks the row, you would have to somehow store that checked state in your "wakeUpTimes" object in the appropriate spot. Then, when the table is reloaded, you would check that state, and if it's checked, set the accessoryType to be the checkmark for that row.
     
  4. thread starter macrumors newbie

    Joined:
    Feb 22, 2010
    Location:
    Germany
    #4
    Thanks for the quick reply, works great now :)

    For everyone interested in the code :

    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        
        // Configuring the cell
        NSString *time = [wakeUpTimes objectAtIndex:indexPath.row];
        cell.textLabel.text = time;
        
        NSString *_savedTime = [[NSUserDefaults standardUserDefaults] stringForKey:@"savedTime"];
        if([time compare:_savedTime]) {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        
        return cell;
    }
     
  5. macrumors newbie

    Joined:
    Jul 25, 2014
    #5
    how to apply for the multiple selected checkmark?

    Help me for the multiple selected checkmark, you have only done for the single checkmark selected
     
  6. macrumors 6502

    Joined:
    Jun 19, 2009
    #6
    It sounds like you want to be able to make multiple selections in your table view.

    Here's how I've done it:

    1. Declare a mutable set property on your table view controller.

    2. When the user taps a cell, get the data object that cell represents. Check your mutable set to see if the data object is in the set. It's not in the set, add it. If it's in the set, remove it.

    3. When you construct your table cell, check your mutable set for the data object. Use the result of that test to determine what the cell accessory should be and set it that way.

    Before the table view get's popped off the stack you could save your mutable set in NSUserDefaults. When the table view gets loaded again, read the set from NSUserDefaults.

    Hope that helps!
     
  7. macrumors newbie

    Joined:
    Jul 25, 2014
    #7
    Thank You TheWatchfulOne

    As I am newbie, Can you please provide me the example code for the same, so that I can get the clear idea.

    Thanks in Advance.
     

Share This Page