我有一个基于用户输入的其他数组的计算结果(使用不同的公式)的结果数组。我想保存数组,当用户点击“保存结果”按钮。另外,如果他再次输入数组,则应单独保存下一个结果数组,以便有一个可变结果数组数组。在iOS编程中保存/检索数组的最佳方式是什么?
有我的应用程序三个表视图
- 用户通过在特定的一天文本字段
- 结果标题(在此之前,最后的结果)
- 详细成绩表(如结果条目表)
我已经使用NSUserdefaults为此,它保存成功,但是当我再次进入条目数组然后结果数组覆盖以前的结果数组 我想知道如何做到这一点(仅通过NSUserdefaults)。
任何人都可以澄清“正确”的方式来做到这一点与具体的代码示例? 感谢
确定这里是一张我的视图代码 做负载
resultsDict = [[NSMutableDictionary alloc] init];
表视图的方法。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *array = [results objectAtIndex:section];
return [array count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [results count ];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell=nil;
if (cell == nil) {
cell = [[[MedicalAirSizingResultsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//switch for calculating the result values for current section
switch (indexPath.section) {
case 0:
//switch to get the current result value
switch (indexPath.row) {
case 0:
resultValue = [self Altitude_above_sea_level];
break;
case 1:
resultValue = [self Intake_air_temperature];
break;
case 2:
resultValue = [self Relative_Humidity];
break;
}
break;
case 1:
switch (indexPath.row) {
case 0:
resultValue = [self System_Model];
break;
case 1:
resultValue = [self Horsepower];
break;
case 2:
resultValue = [self System_Capacity];
break; ....and so on
case 8:
resultValue = [self R_Suggd_Specs];
break;
}
break;
}
//nsdictionary to add in nsuserdefaults
[vaccumResultsDict setObject:[NSNumber numberWithInt:resultValue] forKey:[NSString stringWithFormat:@"%d%d",indexPath.section, indexPath.row]];
NSArray *array = [results objectAtIndex:indexPath.section];
if ([indexPath section]== 3 && [indexPath row] == [array count]) {
UIButton *btnShowResults = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnShowResults.opaque=YES;
[btnShowResults setBackgroundColor:[UIColor clearColor]];
[btnShowResults setBackgroundImage:[UIImage imageNamed:@"ShowResults.png"] forState:UIControlStateNormal];
[btnShowResults setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnShowResults.frame = CGRectMake(0, 0, 320,40);
[btnShowResults addTarget:self action:@selector(showVaccumSizingSavedResults)forControlEvents:UIControlEventTouchDown];
[cell addSubview:btnShowResults];
}
else{
NSString *str = [array objectAtIndex:indexPath.row];
[(VaccumSizingResultsCell *)cell setData:str andResult:resultValue];
}
return cell;
}
//这个方法会被当用户点击保存按钮resutls这是最后一节 的最后一排叫 - (无效){showVaccumSizingSavedResults
NSUserDefaults *savedVaccumResultDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *savedResultsArray = [[NSMutableArray alloc]init];
[savedResultsArray addObject:vaccumResultsDict];
[vaccumResultsDict release];
[savedVaccumResultDefaults setObject:savedResultsArray forKey:@"savedVaccumSizingResultsKey"];
[savedVaccumResultDefaults synchronize];
GetResult *saveCurrentResult = [[GetResult alloc]initWithNibName:@"GetResult" bundle:nil];
[self.navigationController pushViewController:saveCurrentResult animated:YES];
}
请向我们显示您已拥有的代码。 – PengOne