在我的应用程序中,我在 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];
}
}