0

我正在使用 Core Data 和 Web 服务,我想将我的数据添加到我的表中,但我不知道我应该如何称呼它们,请你帮助我,因为当我使用这种方式时它不起作用。

这是我在 HTTP 类中更新数据库的方法

- (void)updateLocalCardsDataBase:(NSArray*) cardsArray
{
    //check if current user has cards in local database
    NSManagedObjectContext* managedObjectContext = [(AppDelegate*) [[UIApplication sharedApplication] delegate] managedObjectContext];

    for(NSDictionary *cardDic in cardsArray)
    {
        Card *card = [NSEntityDescription insertNewObjectForEntityForName:@"Card" inManagedObjectContext:managedObjectContext];
        card.remote_id = [NSNumber numberWithInt:[[cardDic objectForKey:@"id"] intValue]];
        card.stampNumber = [NSNumber numberWithInt:[[cardDic objectForKey:@"stampNumber"] intValue]];
        card.createdAt = [NSDate dateWithTimeIntervalSince1970:[[cardDic objectForKey:@"createdAt"] intValue]];

        [managedObjectContext lock];
        NSError *error;
        if (![managedObjectContext save:&error])
        {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
            NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
            NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
            if(detailedErrors != nil && [detailedErrors count] > 0) {
            for(NSError* detailedError in detailedErrors) {
                NSLog(@"  DetailedError: %@", [detailedError userInfo]);
            }
        }
        else {
            NSLog(@"  %@", [error userInfo]);
        }
    }
    [managedObjectContext unlock];
}

这是我的桌子:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    // NSManagedObjectContext* managedObjectContext = [(AppDelegate*) [[UIApplication sharedApplication] delegate] managedObjectContext];
    static NSString *CellIdentifier = @"CardsCell";
    CardCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
    if (cell == nil){
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"CardCell" owner:nil options:nil];
    for (id currentObject in objects)
    {
        if([currentObject isKindOfClass:[UITableViewCell class]])
        {
            cell = (CardCell *) currentObject;
            break;
        }
    }

    NSDictionary *f = [_cards objectAtIndex:indexPath.row];

    cell.stampId.text = [f objectForKey:@"stampNumber"];
    NSLog(@"%@fdssfdfddavds",[f objectForKey:@"stampNumber"]);
    cell.createdAt.text = [f objectForKey:@"createdAt"];
    cell.CardId.text = [f objectForKey:@"id"];
    return cell;
}

编辑:

我的问题是如何在UITableView

4

1 回答 1

0

在调用之前[tableView reloadData],您需要先获取数据源。您将获得一组数据模型,而不是NSDictionary. 您可以将我的示例方法(或最适合您的变体)放在最适合您需求的地方,但是这个方法不会过滤或排序模型,它只会获取所有模型。另外,我将把方法放在存储表格视图的视图控制器中:

-(NSArray*)getMycards {
    NSManagedObjectContext *context = [(AppDelegate*) [[UIApplication sharedApplication] delegate] managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Card" inManagedObjectContext:context]; 
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    NSError *error;

    [request setEntity:entityDescription];

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

    // now check if there is an error and handle it appropriatelty
    // I usually return 'nil' but you don't have if you don't want
    if ( error != nil ) {
        // investigate error
    }
    return cards;
}

我建议在放置表格的视图控制器中创建一个属性@property NSArray *cards,这样会更容易管理。我做了一个假设(因为我没有关于您的视图控制器的其他信息,所以在您的视图控制器的头文件(@property UITableView *tableView;)中声明了一个名为“tableView”的属性,根据需要调整命名。

使用上述方法,当您想在加载表数据之前填充数组时:

// you put this block of code anywhere in the view controller that also has your table view
// likely in 'viewDidLoad' or 'viewDidAppear'
// and/or anywhere else where it makes sense to reload the table
self.cards = [self getMyCards];
if ( self.cards.count > 0 )
    [self.tableview reloadData];
else {
   // maybe display an error
}

现在,你cellForRowAtIndexPath应该看起来像

-(UITableViewCell*tableView:tableView cellForRowAtIndexPath {
    UITbaleViewCell *cell = ...;
    // creating the type of cell seems fine to me
    .
    .
    .
    // keep in mind I don't know the exact make up of your card model
    // I don't know what the data types are, so you will have to adjust as necessary
    Card *card = self.cards[indexPath.row];

    cell.stampId.text = [[NSString alloc] initWithFormat:@"%@",card.stamp];
    cell.createdAt.text = [[NSString alloc] initWithFormat:@"%@",card.createdAt];
    // you might want format the date property better, this might end being a lot more than what you want
    cell.CardId.text = [[NSString alloc] initWithFormat:@"%@",card.id];

    return cell;
}

Core Data 非常强大,我强烈推荐Core Data 概述,然后是Core Data 编程指南

于 2013-03-05T15:48:54.710 回答