Cyril
Cyril

Reputation: 2818

Adding and saving data on UITableView

I have a method that is working but its not saving the data that I enter.

This is the code I use to enter data on a

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
    NSString *tempTextField = [alertView textFieldAtIndex:0].text;

        if (!numbers) {
            numbers = [[NSMutableArray alloc] init];

        }
        [[NSUserDefaults standardUserDefaults] setObject:tempTextField forKey:@"Save"];
        [numbers insertObject:tempTextField atIndex:0];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } }

It is using a UIAlertView with a plain text input. I try to use an NSUSerDefaults to save the data with the method above and I'm able to retrieve the data on the viewDidLoad with this code

-(void)viewDidLoad {
[super viewDidLoad];
NSArray *siteNameValue = [[NSUserDefaults standardUserDefaults] valueForKey:@"Save"];

    numbers = [[NSMutableArray alloc] initWithObjects:siteNameValue, nil];
}

But it would only save one of the data that is entered, it doesnt save multiple data. Any leads?

the variable numbers is an NSMutableArray.

Upvotes: 1

Views: 42

Answers (3)

Cyril
Cyril

Reputation: 2818

Solution

This is the method to ADD and SAVE data on the tableview using a UIALertView plain text style.

 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex == 1) {

            NSString *tempTextField = [alertView textFieldAtIndex:0].text;

            if (!numbers) {
                numbers = [[NSMutableArray alloc] init];

            }
            [numbers insertObject:tempTextField atIndex:0];
            [[NSUserDefaults standardUserDefaults] setObject:numbers forKey:@"3"];
            [[NSUserDefaults standardUserDefaults] synchronize];

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }

below is to retrieve it under the viewDidLoad

  NSMutableArray *siteNameValue = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"3"] mutableCopy];


    numbers = siteNameValue;

Upvotes: 0

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52223

You can store a NSMutableArray object created alertView:clickedButtonAtIndex in the user defaults and reuse it in viewDidLoad:

[numbers insertObject:tempTextField atIndex:0];
[[NSUserDefaults standardUserDefaults] setObject:numbers forKey:@"Save"];

You can get the array with same content in viewDidLoad as follows:

NSMutableArray *numbers = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"Save"] mutableCopy];

Upvotes: 1

Grzegorz Krukowski
Grzegorz Krukowski

Reputation: 19872

Problem is you are saving just one NSString, not a NSArray in NSUserDefaults.

You need to save "numbers" (whole NSArray) instead and also load it as an array (additional encapsulation on loading is not necessary).

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
    NSString *tempTextField = [alertView textFieldAtIndex:0].text;

    if (!numbers) {
        numbers = [[NSMutableArray alloc] init];

    }
    [[NSUserDefaults standardUserDefaults] setObject:numbers forKey:@"Save"];
    [numbers insertObject:tempTextField atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
} 
}

-(void)viewDidLoad {
    [super viewDidLoad];
    numbers = [[NSUserDefaults standardUserDefaults] valueForKey:@"Save"];
}

Upvotes: 0

Related Questions