好的,那么在收到反馈后,我正在对我的代码进行此更改,它正在运行,但只是添加和替换第一个代码?
- (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;
}
)
最佳答案
声明你的代码,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];
关于iOS 将数组添加到 Plist 文档根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20396480/