iOS SDK,Objective-C,只能显示一个单元格
我有一个用户输入的文本字段,然后我用NSKeyedArchiver保存它并将其拉出以在表上查看,但每次我在文本字段中输入内容并保存它时,它会替换单元格中的第一项,我希望它将其添加到表中 在NewEntry.m中,我使用iOS SDK,Objective-C,只能显示一个单元格,objective-c,ios,uitableview,Objective C,Ios,Uitableview,我有一个用户输入的文本字段,然后我用NSKeyedArchiver保存它并将其拉出以在表上查看,但每次我在文本字段中输入内容并保存它时,它会替换单元格中的第一项,我希望它将其添加到表中 在NewEntry.m中,我使用 - (IBAction)saveButton:(id)sender { int i = selectedSegment.selectedSegmentIndex; app = [[UIApplication sharedApplication] delegate];
- (IBAction)saveButton:(id)sender {
int i = selectedSegment.selectedSegmentIndex;
app = [[UIApplication sharedApplication] delegate];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:i forKey:@"category"];
if (i == 0) {
// [userDefaults setObject:titlefield.text forKey: @"titlehomework"];
// [userDefaults setObject:detailstextfield.text forKey:@"detailshomework"];
NSMutableArray *contactArray;
contactArray = [[NSMutableArray alloc] init];
[contactArray addObject:titlefield.text];
[contactArray addObject:detailstextfield.text];
[NSKeyedArchiver archiveRootObject:
contactArray toFile:datafilePath];
}
else if(selectedSegment.selectedSegmentIndex == 1) {
[userDefaults setObject:titlefield.text forKey:@"titletextprojects"];
[userDefaults setObject:detailstextfield.text forKey:@"detailsprojects"];
}
else if (selectedSegment.selectedSegmentIndex == 2){
[userDefaults setObject:titlefield.text forKey:@"titletextappointments"];
[userDefaults setObject:detailstextfield.text forKey:@"detailsappointments"];
}
else if (selectedSegment.selectedSegmentIndex == 3){
[userDefaults setObject:titlefield.text forKey:@"titletextevents"];
[userDefaults setObject:detailstextfield.text forKey:@"detailsevents"];
}
else if (selectedSegment.selectedSegmentIndex == 4){
[userDefaults setObject:titlefield.text forKey:@"titletexttodolist"];
[userDefaults setObject:detailstextfield.text forKey:@"detailstodolist"];
}
/*[userDefaults synchronize];
titlefield.text =@" ";
detailstextfield.text =@" ";
NSLog(@"selected segment %i", i);*/
/* UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Task saved!" message:@"You're all done." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, selectedSegment.selectedSegmentIndex];
[alert show];*/
[[NSNotificationCenter defaultCenter] postNotificationName:@"ValuesChanged"
object:self];
[self dismissModalViewControllerAnimated:YES];
}
NewEntry ViewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
if (__managedObjectContext == nil)
{
__managedObjectContext = [(StaticTableAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext_: %@", __managedObjectContext);
}
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
datafilePath = [[NSString alloc] initWithString: [docsDir
stringByAppendingPathComponent: @"data.archive"]];
// Check if the file already exists
if ([filemgr fileExistsAtPath: datafilePath])
{
NSMutableArray *dataArray;
dataArray = [NSKeyedUnarchiver
unarchiveObjectWithFile: datafilePath];
//titlefield.text = [dataArray objectAtIndex:0];
// detailstextfield.text = [dataArray objectAtIndex:1];
}
}
新条目ViewDidUnload:
- (void)viewDidUnload
{
[self setTitleTextfield:nil];
[self setCategory:nil];
[self setDetailstextfield:nil];
[self setTitlefield:nil];
[self setTitlefield:nil];
[self setSelectedSegment:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
在表控制器中,我在这里称之为:
tableview:
- (void)viewDidLoad
{
[super viewDidLoad];
// [self fetchRecords];
if(tablesubtitles == nil && tabledata == nil){
tablesubtitles = [[NSMutableArray alloc]init];
tabledata = [[NSMutableArray alloc] init];
}
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
datafilePath = [[NSString alloc] initWithString: [docsDir
stringByAppendingPathComponent: @"data.archive"]];
// Check if the file already exists
if ([filemgr fileExistsAtPath: datafilePath])
{
NSMutableArray *dataArray;
dataArray = [NSKeyedUnarchiver
unarchiveObjectWithFile: datafilePath];
titlestring = [dataArray objectAtIndex:0 ];
detailsstring = [dataArray objectAtIndex:1];
[tabledata addObject:titlestring];
[tablesubtitles addObject:detailsstring];
}
}
以及:
***********更新***************
因此,在saveButton中,它应该是:
NSMutableArray *contactArray;
contactArray = [NSKeyedUnarchiver
unarchiveObjectWithFile: datafilePath];
// contactArray = [[NSMutableArray alloc] init];
[contactArray addObject:titlefield.text];
[contactArray addObject:detailstextfield.text];
[NSKeyedArchiver archiveRootObject:
contactArray toFile:datafilePath];
// NSDictionary *stuff;
问题是您正在创建一个新的可变数组,在其中放入一项,然后将该数组存档到您的文件中。这将取代以前的一切 要添加新项目,您需要先取消阵列存档,然后添加到该阵列,然后存档新阵列
这里似乎也有一些核心数据代码,如果您打算使用核心数据,那么您自己进行归档没有什么好处 在这个问题中有大量无关的和注释掉的代码。你应该把它编辑到你关心的部分,人们会更愿意阅读。哦,我有点理解。那么,您是指saveButton方法吗?我明白你的意思,但不确定在哪里是的,那是我唯一能看到你使用档案的地方。首先需要像在viewDidLoad方法中那样取消归档,然后添加到该数组中。您可能需要使用unarchiver数组创建一个可变数组,我认为unarchiver返回一个不可变的NSArray。我运行了它并更改了objectAtIndex 2和3,但得到的错误是数组没有第三个索引,没有那么大
NSMutableArray *contactArray;
contactArray = [NSKeyedUnarchiver
unarchiveObjectWithFile: datafilePath];
// contactArray = [[NSMutableArray alloc] init];
[contactArray addObject:titlefield.text];
[contactArray addObject:detailstextfield.text];
[NSKeyedArchiver archiveRootObject:
contactArray toFile:datafilePath];
// NSDictionary *stuff;