使用coredata填充表视图时出错

时间:2011-08-01 12:47:21

标签: iphone

我正在使用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;
}

1 个答案:

答案 0 :(得分:0)

您是否在此对象数组中获得了适当数量的元素?

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

如果没有,请尝试使用

更改谓词
[NSPredicate predicateWithFormat:@"name=%@", name.text];

否则,您的代码对我来说似乎是正确的。