好的,在反馈之后,我正在对我的代码进行更改,它正在工作,但只是添加并替换第一个?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *user_id = [prefs objectForKey:@"user_id"];
NSString *fixture_id = [prefs objectForKey:@"fixture_id"];
// Get path to documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
if ([paths count] > 0)
{
NSString *selectedPath = [[paths objectAtIndex:0]
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@",user_id, fixture_id]];
// Read both back in new collections
self.mySelectionsArray = [NSArray arrayWithContentsOfFile:selectedPath];
NSLog(@"Players Selected = %@", self.mySelectionsArray);
}
NSDictionary *tempDic = [[[self.listOfPlayersArray objectAtIndex:indexPath.section] objectForKey:@"contacts"] objectAtIndex:indexPath.row];
NSString *avatar = [tempDic objectForKey:@"avatar"];
NSString *avatarSmall = [tempDic objectForKey:@"avatar_small"];
NSString *first_name = [tempDic objectForKey:@"first_name"];
NSString *last_name = [tempDic objectForKey:@"last_name"];
NSDictionary *innerDic;
innerDic = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: avatar, avatarSmall, first_name, last_name, nil]
forKeys:[NSArray arrayWithObjects:@"avatar", @"avatar_small", @"first_name", @"last_name", nil]];
self.mySelectionsArray = [NSMutableArray arrayWithObject:[NSDictionary dictionaryWithDictionary:innerDic]];
// [self.mySelectionsArray insertObject:innerDic atIndex:0];
[self.mySelectionsArray addObject:innerDic];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([paths count] > 0)
{
// Path to save array data
NSString *arrayPath = [[paths objectAtIndex:0]
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@",user_id, fixture_id]];
NSLog(@"Path: %@",arrayPath);
// Write array
[self.mySelectionsArray writeToFile:arrayPath atomically:YES];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
现在检查数组并将其拉下来然后添加对象
选择播放器后,plist看起来像这样;
{
"first_name" = Ollie;
"last_name" = Akroyd;
},
{
"first_name" = Ollie;
"last_name" = Akroyd;
}
)
答案 0 :(得分:1)
陈述你的代码,mySelections是空的,我猜你想要添加innerDic;此外,您正在保存mySelections而不是userSelections。 您应该将innerDic添加到mySelections并保存userSelections以解决您的问题。
修改强> 而不是
self.mySelectionsArray = [NSMutableArray arrayWithObject:[NSDictionary dictionaryWithDictionary:innerDic]];
覆盖读取数组,添加对象:
[self.mySelectionsArray addObject:innerDic];
self.mySelectionsArray必须是可变的,所以不是
self.mySelectionsArray = [NSArray arrayWithContentsOfFile:selectedPath];
写
self.mySelectionsArray = [[NSArray arrayWithContentsOfFile:selectedPath] mutableCopy];