在我的应用程序中,我有一个 UITableView 和 2 个按钮“保存”和“加载”。就我而言,当用户保存他或她的曲目时,我的 UITableView 会添加一个带有曲目标题的新单元格,然后当用户点击该单元格时,它将重新加载他们的曲目。这工作正常,但是每当用户保存一首曲目,然后尝试保存另一首曲目时,新添加的单元格只会重新加载一首曲目。因此,我的 tableview 可以有 10 个具有名称的单元格,但是每当您点击一个单元格时,只会保存一首曲目。所以我的问题是,如何保存每个单元格的数据?这是我的保存功能:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:NSStringFromCGPoint(ball.center) forKey:@"ballPosition"];
[userDefaults synchronize];
NSUserDefaults *block1DF = [NSUserDefaults standardUserDefaults];
[block1DF setObject:NSStringFromCGPoint(block1.center) forKey:@"block1Position"];
[block1DF synchronize];
NSUserDefaults *block2DF = [NSUserDefaults standardUserDefaults];
[block2DF setObject:NSStringFromCGPoint(block2.center) forKey:@"block2Position"];
[block2DF synchronize];
NSUserDefaults *block3DF = [NSUserDefaults standardUserDefaults];
[block3DF setObject:NSStringFromCGPoint(block3.center) forKey:@"block3Position"];
[block3DF synchronize];
NSUserDefaults *winBlockDF = [NSUserDefaults standardUserDefaults];
[winBlockDF setObject:NSStringFromCGPoint(winBlock.center) forKey:@"winBlockPosition"];
[winBlockDF synchronize];
NSString *trackName = [[alertView textFieldAtIndex:0] text];
trackTitle.text = trackName;
[trackArray addObject:[NSString stringWithFormat:trackName,[trackArray count]+1]];
[self.myTableView reloadData];
然后我的 tableViewdidSelectRowAtIndexPath:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
CGPoint ballPoint = CGPointFromString([userDefaults objectForKey:@"ballPosition"]);
[ball setCenter:ballPoint];
NSUserDefaults *block1DF = [NSUserDefaults standardUserDefaults];
CGPoint block1Point = CGPointFromString([block1DF objectForKey:@"block1Position"]);
[block1 setCenter:block1Point];
NSUserDefaults *block2DF = [NSUserDefaults standardUserDefaults];
CGPoint block2Point = CGPointFromString([block2DF objectForKey:@"block2Position"]);
[block2 setCenter:block2Point];
NSUserDefaults *block3DF = [NSUserDefaults standardUserDefaults];
CGPoint block3Point = CGPointFromString([block3DF objectForKey:@"block3Position"]);
[block3 setCenter:block3Point];
NSUserDefaults *winBlockDF = [NSUserDefaults standardUserDefaults];
CGPoint winBlockPoint = CGPointFromString([winBlockDF objectForKey:@"winBlockPosition"]);
[winBlock setCenter:winBlockPoint];