开发者

TableView doesnt show any Data(CoreData) - App crashe

开发者 https://www.devze.com 2023-02-03 05:00 出处:网络
i cant read my data from my database. I have an app with a tabbarcontroller. in the first tab the iphone camera takes a picture from a barcode and send the result to another view (CameraReturnDetailVi

i cant read my data from my database. I have an app with a tabbarcontroller. in the first tab the iphone camera takes a picture from a barcode and send the result to another view (CameraReturnDetailViewController). In CameraReturnDetailViewController is the savebutton, and here is the code from this save button:

- (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 dismissModalViewC开发者_运维技巧ontrollerAnimated:YES];

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

Now, my aim is to show the taba in the second tab, in a TableView (ScansViewController):

- (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_;
}  

At first i get this error when i choosed the second tab(ScansViewController): " Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'BarcodeDaten'' "

The Name is correct but i dont understand my mistake.

No data is showed in the Tableview, why?

Have I missed something..? Or something wrong?

Thanks for help, brush51


This is the solution:

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

this code is from here: Core-Data iPhone: could not locate an NSManagedObjectModel

0

精彩评论

暂无评论...
验证码 换一张
取 消