TableView不显示任何数据(核心数据) - 应用程序崩溃

时间:2011-01-05 16:49:46

标签: uitableview core-data ios4 nsfetchedresultscontroller nsfetchrequest

我无法从我的数据库中读取数据。我有一个带tabbarcontroller的应用程序。 在第一个标签中,iphone相机从条形码中拍摄照片并将结果发送到另一个视图(CameraReturnDetailViewController)。 在CameraReturnDetailViewController中是保存按钮,这是来自此保存按钮的代码:

- (IBAction)saveAndQuitScan:(id) sender {
    XLog(@"saveAndQuitScan button wurde geklickt!");

    ProjectQRCodeAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSManagedObject *newData;

    newData = [NSEntityDescription insertNewObjectForEntityForName:@"BarcodeDaten"     inManagedObjectContext:context];

    [newData setValue:dataLabel.text forKey:@"Barcode_CD"];

    NSError *error;
    [context save:&error];

    //Aktuelle ansicht (self) animiert verlassen
    [self dismissModalViewControllerAnimated:YES];

    // Nachdem die ansicht verlassen wurde, 
    // auf das zweite Tab wechseln(scanverlauf)
    /** TO DO - Funktioniert noch nicht **/
    [self.tabBarController setSelectedIndex:1]; 
}  

现在,我的目标是在TableView(ScansViewController)的第二个选项卡中显示taba:

- (void)viewDidLoad {
[super viewDidLoad];

if (managedObjectContext_ == nil) 
    { 
managedObjectContext_ = [(ProjectQRCodeAppDelegate *)[[UIApplication sharedApplication]delegate] managedObjectContext]; 
NSLog(@"After managedObjectContext: %@",  managedObjectContext_);
}

myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
myTableView.delegate = self;
myTableView.dataSource = self;

myTableView.autoresizesSubviews = YES;

    self.navigationItem.title = @"Code Liste";

    self.view = myTableView;    

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
    return [itemsList count];
}

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

    static NSString *CellIdentifier = @"Cell";

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

return cell;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *selectDay = [NSString stringWithFormat:@"%d", indexPath.row];

    TableDetailViewController *fvController = [[TableDetailViewController alloc] initWithNibName:@"TableDetailViewController" bundle:[NSBundle mainBundle]];
    fvController.selectDay = selectDay;
    [self.navigationController pushViewController:fvController animated:YES];
    [fvController release];
    fvController = nil;

}

- (void) configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[managedObject valueForKey:@"Barcode_CD"] description];
}

- (NSFetchedResultsController *) fetchedResultsController {


    if (fetchedResultsController_ !=nil) {
        return fetchedResultsController_;
    }


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"BarcodeDaten"     inManagedObjectContext:self.managedObjectContext];

    [fetchRequest setEntity:entity];
    [fetchRequest setFetchBatchSize:20];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Barcode_CD"     ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]     initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext     sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

NSError *error = nil;
    if (![fetchedResultsController_ performFetch:&error]) {
        XLog(@"Error: %@, %@", error, [error userInfo]);
        abort();
    }

return fetchedResultsController_;
}  

首先,当我选择第二个选项卡(ScansViewController)时,我收到此错误: “ 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'+ entityForName:无法找到实体名称'BarcodeDaten'的NSManagedObjectModel' “

姓名是正确的,但我不理解我的错误。

Tableview中没有显示数据,为什么?

我错过了什么......?还是错了?

感谢您的帮助, brush51

1 个答案:

答案 0 :(得分:3)

这是解决方案:

if (managedObjectContext == nil) 
{ 
        managedObjectContext = [(CoreDataBooksAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
        NSLog(@"After managedObjectContext: %@",  managedObjectContext);
}

此代码来自此处:Core-Data iPhone: could not locate an NSManagedObjectModel