我正在使用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
答案 0 :(得分: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 overview,然后是Core Data Programming Guide。