maddy
maddy

Reputation: 4111

how to save the state in userdefaults of accessory checkmark-iphone

I am working on an application having UItableView. 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 NSUSerDefaults 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:

MY.h file

**@protocol LocationSelectionViewControllerDelegate <NSObject>

@required
- (void)rowSelected:(NSString *)selectedValue selectedIndex:(NSInteger)index;
@end**

@interface LocationSelection : UIViewController <UITableViewDelegate,UITableViewDataSource>{

UITableView *table;
***NSInteger       selectedIndex;***
NSMutableArray *menuList;
***id <LocationSelectionViewControllerDelegate>    delegate;***

}
@property (nonatomic,retain) NSMutableArray *menuList;  
@property (nonatomic,retain) IBOutlet UITableView *table;  
***@property (nonatomic, assign) id <LocationSelectionViewControllerDelegate> delegate;**  
**@property NSInteger selectedIndex;***  
@end  

my .m file:

@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 && selectedIndex > -1 && selectedIndex < [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 > - 1 && selectedIndex < [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];
    }
}

Upvotes: 2

Views: 1234

Answers (3)

Nick Weaver
Nick Weaver

Reputation: 47231

Put a state in your menuList which reflects selection of the cell so even multiselection can be done easily. You can use a dictionary instead of the arrays. Arrays are fine but not as readable, so you have to remember which field contains what and map it to an index.

When the view loads load the menuList array from your userDefaults, when the app closes save the defaults.

In didSelectRowAtIndexPath you save selection in menuList.

In cellForRowAtIndexPath you read menuList and the new array index/dictionary key and set the checkmark.

Upvotes: 0

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

Use this method to save your data to NSUserDefaults And set the data in viewDidLoad of LocationSelection

Upvotes: 1

visakh7
visakh7

Reputation: 26390

Hi I think it will suit you if you have an object(s) to indicate the checkmark. You can save this to the user defaults through synchronize. In the cellForRowATIndexPath: you can check if the value is present in the user defaults and if yes make the cell accessory as checkmarked and if its not present make it none.

Upvotes: 0

Related Questions