删除NSUserDefault iOS中保存的表格行

时间:2013-06-11 12:00:17

标签: ios objective-c nsuserdefaults

在我的应用程序中,我在AlertView文本字段的帮助下为我的PDF文件命名。这些名称保存在表格列表和NSUserDefault中以便随时访问。现在,当我删除单行表时,整个NSUserDefault列表都消失了。你能否在我的代码中提出一些技巧。提前谢谢。

更新了代码(viewDidLoad)并带有评论

- (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];

}
}

1 个答案:

答案 0 :(得分:1)

据我了解,您在使用

时删除所有默认设置
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

只需删除此行代码

即可