Navnath Memane
Navnath Memane

Reputation: 273

Deleting table row saved in NSUserDefualt iOS

In my application I am giving name to my PDF file with the help of AlertView textfield. The names are saving in Array of table list as well as NSUserDefault to access at any time. Now when I am deleting the single row of table the whole list of NSUserDefault is getting disappeared. Could you please suggest some trick over here in my code. Thanks in advance.

Updated code(viewDidLoad) with comments

- (void)viewDidLoad
{
[super viewDidLoad];
  NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
 //Checking previous NSUserDefault. Here I need trick to open the previous updated     NSUserDefault

 if([[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@",[self.tablePdfListArray objectAtIndex:indexPath.row]]] != nil)
 {
 self.tablePdfListArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@",[self.tablePdfListArray objectAtIndex:indexPath.row]]]];

 }

 }
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0)
{
 if(!self. tablePdfListArray)
    {

        self.tablePdfListArray = [[NSMutableArray alloc]init];
    }

//the below if condition will not allow repeatative string array in tableList and textfield lenth.

    if ([[alertView textFieldAtIndex:0].text length] != 0 && ![self.tablePdfListArray containsObject:self.myPDFName])
    {

        [self.tablePdfListArray insertObject:[NSString stringWithFormat:@"%@", [alertView textFieldAtIndex:0].text] atIndex:0];

        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

        [self.pdfListnameTable insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

        //adding table Array in NSUserDefaults

        NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
        [defaults setObject:self.tablePdfListArray forKey:[NSString stringWithFormat:@"%@.",[self.tablePdfListArray objectAtIndex:indexPath.row]]];
        [defaults synchronize];


    }
}

- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {

    //Deleting single table row. The NSUserDefault should be updated here.

   // NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
   // [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];


    [[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"%@.",[self.tablePdfListArray objectAtIndex:indexPath.row]]];

    [pdfListnameTable beginUpdates];
    [pdfListnameTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [pdfListnameTable deleteSections:indexes withRowAnimation:UITableViewRowAnimationFade];
    [pdfListnameTable endUpdates];

}
}

Upvotes: 0

Views: 154

Answers (1)

nerowolfe
nerowolfe

Reputation: 4817

As I understand you are removing all defaults when using

[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

Just remove this line of code

Upvotes: 1

Related Questions