2012-02-26 21 views
0

我有一個用戶輸入的文本框,然後用NSKeyedArchiver保存並將其保存在桌面上查看,但每次我在文本框中輸入內容並保存它取代了單元格中的第一個項目,我希望它將它添加到表格中。iOS SDK,Objective-C,可用只顯示一個單元格

在NewEntry.m,我保存

- (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 [email protected]" "; 
detailstextfield.text [email protected]" "; 
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]; 
    } 






} 

和:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 

UITableViewCell *cell = nil; 

cell = [tableView dequeueReusableCellWithIdentifier:@"homeworkcell"]; 


if(cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"homework"]; 

} 



NSString *cellValue = [tabledata objectAtIndex:indexPath.row]; 

NSString *subtitle = [tablesubtitles objectAtIndex:indexPath.row]; 




cell.textLabel.text= cellValue; 

cell.detailTextLabel.text= subtitle; 

cell.textLabel.font = [UIFont systemFontOfSize:14.0]; 
cell.textLabel.backgroundColor = [ UIColor clearColor]; 
cell.detailTextLabel.backgroundColor = [UIColor clearColor];  



    //[tabledata addObjectser.title]; 
    //[tabledata addObject:user.detai/s]; 

//static NSString *CellIdentifier = @"Cell"; 



// Configure the cell. 

//-----------------------------------------START----------------------------Set image of cell---- 
cellImage = [UIImage imageNamed:@"checkboxblank.png"]; 

cell.imageView.image = cellImage; 

//--------------------------------------------END---------------------------end set image of cell-- 

return cell; 


} 

** * ** * ** * ** * *UPDATE* ** * ** * ** * 所以在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; 

+0

在這個問題中有大量無關和註釋掉的代碼。你應該把它編輯到你關心的部分,然後人們會更願意閱讀它。 – jrturton 2012-02-26 15:52:39

回答

1

問題是您正在創建一個新的可變數組,將一個項目放入其中,然後將該數組歸檔到您的文件中。這將取代之前的任何東西。

要添加一個新項目,您需要首先取消存檔數組,然後添加到該數組,然後存檔您的新數組。

這裏似乎也有一些核心數據代碼,如果您打算使用核心數據,那麼在做自己的歸檔時沒有什麼價值。

+0

哦,我有點理解。那麼你的意思是在saveButton方法中?我明白你的意思,但不知道在哪裏 – franklinexpress 2012-02-26 15:59:22

+0

是的,這是我能看到你使用存檔器的唯一地方。您需要先像viewDidLoad方法那樣解壓縮,然後將其添加到該數組中。你可能需要用unarchiver數組創建一個可變數組,我認爲unarchiver返回一個不可變的NSArray。 – jrturton 2012-02-26 16:00:30

+0

好的檢查我的編輯,我認爲這就是你的意思是正確的? – franklinexpress 2012-02-26 16:18:32

相關問題