designer
designer

Reputation: 45

When user adds row to UITableView, the new row does not "stay" when returning to the app

I am working on an app where the user can estimate various length and width dimensions (area) for better determining required quantities.

I am struggling with a few items, but will address them one at a time...

I would like the user to add as many fields as they like to input the required areas they need calculated... for this I am using a UITableView and a reuseable cell... I have coded to allow the user to add a new row (or as many rows as they like). Currently the app starts with 3 rows, but if they want to add more they can.

So that "part" is working... the user can add and delete rows at will, however, if a user adds 3 or 4 more rows in the app and then closes the app, the app on return only displays the default number of rows.

My question is... how can the app "remember" those additional rows...so the next time the user enters the app, the new number of rows are always shown and not my default. I am not currently asking how to save the data or information inside the row, simply how to save the new number of rows for use later on...

I have some screen caps for reference...

You can see I start with the default 3 rows, and allow the user to add more... that does function... however, when I close the app, the default 3 rows show again, forgetting the rows added by the user... any help much appreciated...

enter image description here

enter image description here

enter image description here

UPDATE

So now I have the following additional code based on the suggestions posted below...

In controller

- (UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

SodTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
    cell = [[SodTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
//if number of rows is greater than the total number of rows in the data set
//and this view is in editing mode.
//Initialize the cell for "Add Row"
//there will be an extra row once SetEditing: is called
if(indexPath.row >= tabledata.count && [self isEditing]){

    cell.areaNumber.text = @"";

    NSUserDefaults *area = [NSUserDefaults standardUserDefaults];
    [area setObject:self.area_Input forKey:@"sod_area_input_by_user"];
    [area synchronize];
    NSLog(@"%@",[area valueForKey:@"sod_area_input_by_user"]);

}else{ // this tableview is showing table (not in edit mode)

    /*cell.areaNumber.text = [tabledata objectAtIndex:indexPath.row];*/
    cell.areaNumber.text = tabledata[indexPath.row];

    // Configure the cell...
    UILabel*label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2.5, 0, self.view.bounds.size.width, 44)];

    NSString*text = [NSString stringWithFormat:@"Area %ld",(long)indexPath.row+1];
    [label setText:text];
    [label setTextColor: [UIColor colorWithRed:0.41 green:0.33 blue:0.25 alpha:1.0]];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:14.0f];
    [cell.contentView addSubview: label];
}

return cell;

}

Where area_Input is a NSString...

I am trying to then show the result here in viewDidLoad

tabledata = [[NSMutableArray alloc] initWithObjects:self.area_Input, nil];

But it appears as nil when viewing the NSLog... any ideas?

Upvotes: 0

Views: 72

Answers (2)

designer
designer

Reputation: 45

Solved...

In case this helps anyone else...

- (UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

SodTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
    cell = [[SodTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

//if number of rows is greater than the total number of rows in the data set and this view is in editing mode.
//Initialize the cell for "Add Row"
//there will be an extra row once SetEditing: is called
if(indexPath.row >= tabledata.count && [self isEditing]){

    cell.areaNumber.text = @"";


}else{ // this tableview is showing table (not in edit mode)

    cell.areaNumber.text = [tabledata objectAtIndex:indexPath.row];
    //cell.areaNumber.text = tabledata[indexPath.row];
    //[cell addSubview:cell.areaNumber];

    NSUserDefaults *save = [NSUserDefaults standardUserDefaults];
    [save setObject:tabledata forKey:@"sod_area_input_by_user"];
    [save synchronize];
    NSLog(@"This number is from cell creation code %@",[save valueForKey:@"sod_area_input_by_user"]);

    // Configure the cell...
    UILabel*label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2.5, 0, self.view.bounds.size.width, 44)];

    NSString*text = [NSString stringWithFormat:@"Area %ld",(long)indexPath.row+1];
    [label setText:text];
    [label setTextColor: [UIColor colorWithRed:0.41 green:0.33 blue:0.25 alpha:1.0]];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:14.0f];
    [cell.contentView addSubview: label];

}

return cell;

}

Then in viewDidLoad...

- (void)viewDidLoad

{ [super viewDidLoad];

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"+ / -" style: UIBarButtonItemStylePlain target:self action:@selector(addORDeleteRows)];[self.navigationItem setRightBarButtonItem:addButton];

// Code to adjust the background image used in app based on whether it is an ipad or iphone 4 or iphone 5

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

    // iPad code

    self.view.backgroundColor = [UIColor clearColor];
    [ADVThemeManager customizeView:self.view];

}

tabledata = [[NSMutableArray alloc] init];

if([tabledata count]==0){
    //Your array is empty so you have to call it with @"yourKey"
    tabledata=[[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]objectForKey:@"sod_area_input_by_user"]];
}

    NSUserDefaults *save = [NSUserDefaults standardUserDefaults];
    [save setObject:tabledata forKey:@"sod_area_input_by_user"];

numberOfSection = 1;

[myTable reloadData];

Now the app will allow me to insert and delete rows as needed...

I think it should be noted that "tabledata" is a NSMutableArray

Thanks everyone for your input... much appreciated...

Upvotes: 0

Omar Masri
Omar Masri

Reputation: 359

You can save these cells info in NSUserDefaults then in your viewWillAppear retrieve these cells and save them to an array that you use to load the cells.

Upvotes: 1

Related Questions