2012-02-25 12 views
0

私はこの問題を何時間も解決し、このコードで何が間違っているかを調べようとしています。私はテーブルのセルにタイトルとサブタイトルを保存するためにコアデータを使用しています。 私のアプリケーションデリゲート(sharedAppDelegate)にいくつかの設定があり、私のセルにデータを挿入しようとしていました。ここで Objective-C、iOS、Core Data、Table Cells empty

は、私は私のappDelegate.mに持っているものです。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSManagedObjectContext *cxt = [[StaticTableAppDelegate sharedAppDelegate] managedObjectContext]; 
    NSError *error; 

    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Homework" inManagedObjectContext:cxt]; 
    [request setEntity:entity]; 
    NSArray *arry = [cxt executeFetchRequest:request error:&error]; 
for(UserData *user in arry){ 
    NSLog(@"title %@", user.title); 
    NSLog(@"details %@", user.details); 

    } 


    StaticTableAppDelegate *ad = [StaticTableAppDelegate sharedAppDelegate]; 
    NSArray *list = [ad allConsoles]; 


    if ([list count]==0) { 

    tabledata = [[NSMutableArray alloc] init]; 
    tablesubtitles = [[NSMutableArray alloc]init]; 

     [tabledata addObject:@"hello2"]; 
     [tabledata addObject:@"hello2"]; 
     [tabledata addObject:@"hello2"]; 
     [tabledata addObject:@"hello2"]; 


     [tablesubtitles addObject:@"details"]; 
     [tablesubtitles addObject:@"details"]; 
     [tablesubtitles addObject:@"details"]; 
     [tablesubtitles addObject:@"details"]; 


     NSManagedObjectContext *moc = [ad managedObjectContext]; 


    for (int i=0; i<[tabledata count]; i++) { 

    NSManagedObject *newTabledata = [NSEntityDescription insertNewObjectForEntityForName:@"Homework" inManagedObjectContext:moc]; 

    [newTabledata setValue:[NSString stringWithFormat:[tabledata objectAtIndex:i]] forKey:@"title"]; 


    NSManagedObject *newTablesub = [NSEntityDescription insertNewObjectForEntityForName:@"Homework" inManagedObjectContext:moc]; 
    [newTablesub setValue:[NSString stringWithFormat:[tablesubtitles objectAtIndex:i]] forKey:@"details"]; 


    } 


    list = [ad allConsoles]; 
    } 


tabledata = [tabledata mutableCopy]; 

} 

そしてテーブルビュー方法:

+(StaticTableAppDelegate *)sharedAppDelegate 
{ 
return sharedInstance; 

} 

-(NSArray *)allConsoles 
{ 
NSManagedObjectContext *moc = [[StaticTableAppDelegate sharedAppDelegate] managedObjectContext]; 
NSFetchRequest *fetch = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Homework" inManagedObjectContext:moc]; 
[fetch setEntity:entity]; 

//sort 
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES]; 
NSArray *sortdescriptors = [NSArray arrayWithObject:sd]; 
[fetch setSortDescriptors:sortdescriptors]; 

NSError *error; 
NSArray *result = [moc executeFetchRequest:fetch error:&error]; 
if(!result){ 
    NSLog(@"%@", [error localizedDescription]); 
    return nil; 
} 
return result; 

} 

そして、私はこれを持っているビューに(私は、アプリのデリゲートをインポート)

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

UITableViewCell *cell = nil; 

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


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

} 





cell.textLabel.text=[[tabledata objectAtIndex:indexPath.row] valueForKey:@"title"]; 
cell.detailTextLabel.text = [[tablesubtitles objectAtIndex:indexPath.row] valueForKey:@"details"]; 
cell.textLabel.font = [UIFont systemFontOfSize:14.0]; 
cell.textLabel.backgroundColor = [ UIColor clearColor]; 
cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 

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

cell.imageView.image = cellImage; 

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

return cell; 

}

私は助けに感謝します。私はあなたがcoredataに保存するcoredata..Becauseあなたのコードの中に保存して、問題を持つ願ってい

+0

されます完全に役に立たず、正しい方法ではありません。 –

+0

これは役に立たないコメントでしたが、感謝しています。 –

+0

あなたの質問は少し不明です。データの保存やデータの表示に問題がありますか? – sosborn

答えて

0

これは、uはあなたのコードのためにそれを変更することができれば.... coreDataに保存する方法を..です

MortgageAppAppDelegate *MortDataObj=(MortgageAppAppDelegate *)[[UIApplication sharedApplication]delegate]; 

    NSManagedObjectContext *context = [MortDataObj managedObjectContext]; 

    loanDetails=[NSEntityDescription 
           insertNewObjectForEntityForName:@"LoanDetails" 
           inManagedObjectContext:context]; 
    loanDetails.loanName = name; 
    loanDetails.loanDescription = desc; 


    NSError *error; 
    if (![context save:&error]) { 
     NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); 
    } 
+0

うわー、ありがとう。それが働いた –

+0

今すぐunarchiverを使用する時間..私はすべてのメソッドを学ぶしようとしている... –

+0

それは素晴らしい..先に行く.. –

関連する問題