0

私のアプリケーションでは、AlertView テキストフィールドを使用して PDF ファイルに名前を付けています。名前はテーブル リストの配列と NSUserDefault に保存され、いつでもアクセスできます。テーブルの1行を削除すると、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];

}
}
4

1 に答える 1

1

私が理解しているように、使用時にすべてのデフォルトを削除しています

[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

このコード行を削除するだけです

于 2013-06-11T12:33:48.423 に答える