Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9165965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:02:42+00:00 2026-06-17T15:02:42+00:00

I am trying to save any of the items as user selects from a

  • 0

I am trying to save any of the items as user selects from a uitableview to nsuserdefaults. At that moment only the most recent selection is saved. I would like to have the user to be able to select any of the rows they want and then saved to nsuserdefaults and then use that info anywhere in the app.

thanks for any help

here’s my code:

   - (void)viewDidLoad
{
    [super viewDidLoad];


    // categories array
    listOfCategories = [[NSMutableArray alloc] init];

    [listOfCategories addObject:@"Food & Drinks"];
    [listOfCategories addObject:@"Beauty & Wellness"];
    [listOfCategories addObject:@"Sports & Fun Activities"];
    [listOfCategories addObject:@"Labor & Services"];
    [listOfCategories addObject:@"Clothes & Accessories"];
    [listOfCategories addObject:@"Education & Training"];
    [listOfCategories addObject:@"Products"];

    // add label
    UIView *viewForHeader = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,40)];
    UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,0,80,30)];
    categoryLabel.text = @"Select all:";

    [categoryLabel setFont:[UIFont fontWithName: @"Asap-Bold" size: 14.0f]];
    categoryLabel.textColor = [UIColor lightGrayColor];

    // add switch
    onoff = [[UISwitch alloc] initWithFrame: CGRectMake(100.0f, 0.0f, 100.0f, 0.0f)];
    [onoff addTarget: self action: @selector(flipSwitch:) forControlEvents:UIControlEventValueChanged];

    [viewForHeader addSubview:onoff];

    [viewForHeader addSubview:categoryLabel];
    self.tableView.tableHeaderView = viewForHeader;

}

// uiswitch button
- (IBAction) flipSwitch: (id) sender {
    onoff = (UISwitch *) sender;
    NSLog(@"%@", onoff.on ? @"On" : @"Off");

    if (onoff.on) {
        for (NSInteger s = 0; s < self.tableView.numberOfSections; s++) {
            for (NSInteger r = 0; r < [self.tableView numberOfRowsInSection:s]; r++) {
                [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]
                                            animated:NO
                                      scrollPosition:UITableViewScrollPositionNone];
            }
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [listOfCategories count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }        // Configure the cell...

    NSString *cellValue = [listOfCategories objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    [cell.textLabel setFont:[UIFont fontWithName: @"Asap-Bold" size: 14.0f]];

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    int index = indexPath.row; id obj = [listOfCategories objectAtIndex:index];


    //This toggles the checkmark
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        UIImage *image = [UIImage imageNamed:@"icon-tick.png"];

        UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [downloadButton setImage:image forState:UIControlStateNormal];
        [downloadButton setFrame:CGRectMake(0, 0, 19, 19)];
        [downloadButton setBackgroundColor:[UIColor clearColor]];
        [tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        //This sets the array

    } else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;

        UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [downloadButton setTitle:@"" forState:UIControlStateNormal];
        [downloadButton setFrame:CGRectMake(0, 0, 0, 0)];
        [tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;


    }

    // Save text of the selected cell:
    UITableViewCell *cellSelected = [tableView cellForRowAtIndexPath:indexPath];

    if ([cellSelected.textLabel.text isEqualToString:@"Food & Drinks"]) {
        NSString *valueToSave = cellSelected.textLabel.text;
        [[NSUserDefaults standardUserDefaults]
         setObject:valueToSave forKey:@"preferenceName"];

    }

    NSString *valueToSave = cellSelected.textLabel.text;
    [[NSUserDefaults standardUserDefaults]
     setObject:valueToSave forKey:@"preferenceName"];


    NSString *savedValue = [[NSUserDefaults standardUserDefaults]
                            stringForKey:@"preferenceName"];

    NSLog(@"savedValue %@", savedValue);

    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    // Customize archiver here
    [archiver encodeObject:obj forKey:@"keyForYourArrayOfNSIndexPathObjects"];
    [archiver finishEncoding];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"keyForYourArrayOfNSIndexPathObjects"];


    NSKeyedUnarchiver *unarchiver;
    unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:
                  [[NSUserDefaults standardUserDefaults] objectForKey:@"keyForYourArrayOfNSIndexPathObjects"]];
    // Customize unarchiver here
    categoryItemSelected = [unarchiver decodeObjectForKey:@"keyForYourArrayOfNSIndexPathObjects"];
    [unarchiver finishDecoding];

    NSLog(@"list of categories selected %@", categoryItemSelected);


}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:02:44+00:00Added an answer on June 17, 2026 at 3:02 pm

    The issue is because you overridden the previously saved data.
    So first of all you need to load saved data:

    NSKeyedUnarchiver *unarchiver;
    unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:
                  [[NSUserDefaults standardUserDefaults] objectForKey:@"keyForYourArrayOfNSIndexPathObjects"]];
    // Customize unarchiver here
    categoryItemSelected = [unarchiver decodeObjectForKey:@"keyForYourArrayOfNSIndexPathObjects"];
    [unarchiver finishDecoding];
    if (categoryItemSelected == nil) {
        //If it isn't been created - then create new array
    }
    

    Then add new object:

    [categoryItemSelected addObject: obj];
    

    And then save it back to UserDefautls

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to display and capture only the most recent item from a
Im trying to save a simple script in netbeans to htdocs, but i only
When trying to save the database using boost serialization, I encounter the segfault that
I'm trying to save my Photo class that has a byte[] File field. When
I'm trying to save Images the user takes with the camera 1) If I
i'm trying to create a list of the items from my database. the problem
I am trying to save data from a listview control to Microsoft SQL Server.
I'm trying to have a View where the user can add items in a
im trying to save a row in my settings table, it works all fine
I'm trying to save some data into an array but unfortunately all the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.