2011-08-01 1 views
0

私はcoredataを使って作業していて、データストレージにデータを正常に保存しましたが、テーブルビューを使用して保存したものを出力しようとしました。テーブルビューにコアデータを取り込むときのエラー

ここで、ユーザーは入力し、[戻る]をクリックして[保存]をクリックします。入力した名前で表を作成します。

だから私は、保存のために以下のコードを書いて、データを見つける:

- (void) saveData 
{ 
    coredata123AppDelegate *appDelegate = 
    [[UIApplication sharedApplication] delegate]; 

    NSManagedObjectContext *context = 
    [appDelegate managedObjectContext]; 
    Contacts *newContact; 
    newContact = [NSEntityDescription 
        insertNewObjectForEntityForName:@"Contacts" 
        inManagedObjectContext:context]; 
    [newContact setValue:name.text forKey:@"name"]; 
    [newContact setValue:address.text forKey:@"address"]; 
    [newContact setValue:phone.text forKey:@"phone"]; 
    name.text = @""; 
    address.text = @""; 
    phone.text = @""; 
    NSError *error; 
    [context save:&error]; 
    status.text = @"Contact saved";  
} 

- (void) findContact 
{ 
    coredata123AppDelegate *appDelegate = 
    [[UIApplication sharedApplication] delegate]; 
    NSManagedObjectContext *context = 
    [appDelegate managedObjectContext]; 
    NSEntityDescription *entityDesc = 
    [NSEntityDescription entityForName:@"Contacts" 
       inManagedObjectContext:context]; 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entityDesc]; 
    NSPredicate *pred = 
    [NSPredicate predicateWithFormat:@"(name = %@)", 
    name.text]; 
    [request setPredicate:pred]; 
    NSManagedObject *matches = nil; 
    NSError *error; 
    NSArray *objects = [context executeFetchRequest:request 
               error:&error]; 

    for(Contacts *info in objects) 
    { 
     NSLog(@"Name:%@",info.name); 
     NSLog(@"Address:%@",info.address); 
    } 
    if ([objects count] == 0) { 
     status.text = @"No matches"; 
    } else { 
     matches = [objects objectAtIndex:0]; 
     address.text = [matches valueForKey:@"address"]; 
     phone.text = [matches valueForKey:@"phone"]; 
     status.text = [NSString stringWithFormat: 
         @"%d matches found", [objects count]]; 
    } 
    [request release]; 


} 

と私は、このコードを書かれているテーブルビューを埋めるために...

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription 
            entityForName:@"Contacts" inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 
    NSError *error; 
    self.contacts = [context executeFetchRequest:fetchRequest error:&error]; 

    [fetchRequest release]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 


    Contacts *info =[contacts objectAtIndex:indexPath.row]; 
    cell.textLabel.text =info.name; 
    cell.detailTextLabel.text=info.address; 

    // Configure the cell... 

    return cell; 
} 
+0

こんにちは友人ウルreply..iため – Ranjit

答えて

0

あなたは要素の適切な数を取得していますこのオブジェクト配列では?

NSArray *objects = [context executeFetchRequest:request 
              error:&error]; 

ない場合は、と述語を変更してみてください、

[NSPredicate predicateWithFormat:@"name=%@", name.text]; 

そうでない場合は、あなたのコードは、私には正しいようです。

+0

OKおかげカリムを待っメートル屋ハロー配列 – Ranjit

+0

それをしようとします.......返信してください私は、テーブルビューにデータを出力する方法を見つけることに私を出す.... – Ranjit

+0

でeveryone..pleaseヘルプを要素の適切な数を取得karim..i – Ranjit

関連する問題