2012-02-26 6 views
0

ユーザーが入力したテキストフィールドがある場合、NSKeyedArchiverで保存してテーブルに表示しますが、テキストフィールドに何かを入力して保存しますそれは、セルの最初の項目を置き換えます。テーブルに追加する必要があります。iOS SDK、Objective-C、1つのセルしか表示しない

- (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; 

} 

そして、私はそれを呼び出すテーブルコントローラで、私は

- (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でデータを保存NewEntry.mで

ここ: テーブルビュー:

- (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

答えて

1

問題は、新しい可変配列を作成し、その中に1つの項目を入れて、その配列をファイルにアーカイブすることです。これは前にあったものを置き換えるものです。

新しい項目を追加するには、最初に配列のアーカイブを解除し、その配列に追加してから新しい配列をアーカイブする必要があります。

ここでもコアデータコードがあるようですが、コアデータを使用する場合は独自のアーカイブを行うメリットはほとんどありません。

+0

ああ、私は理解しています。 saveButtonメソッドの意味ですか?私はあなたが何を意味するのかはわかっていますが、どこにいないのかわかりません – franklinexpress

+0

はい、アーカイバをどこで使うのが見えるのですか。最初にviewDidLoadメソッドのように解凍し、その配列に追加する必要があります。あなたはunarchiver配列で可変配列を作る必要があるかもしれません、私はunarchiverが不変のNSArrayを返すと思います。 – jrturton

+0

私の編集を確認してください、それは正しいと思いますか? – franklinexpress

関連する問題