核心数据深入研究表

时间:2014-03-25 14:24:06

标签: objective-c core-data

我正在与Core Data合作,我希望你可以帮助我解决我遇到的问题。举例来说,我有3个实体Make和Model,Options,以及具有多个属性的汽车图像。每个选项可以有1个Make,多个选项和1个图像。我想要发生的是,如果只有一个选项跳过钻取中的选项并直接进入图像。例如:

Table of more than one Option
make - Model | Options | Photo
Chevy Camaro  ->  LS  ->  Image of Car.
Chevy Camaro  ->  LT  ->  Image of Car.

table of one Option 

Chevy Blazer  -> Image of Car

这就是我设想的方式

这是if语句

if(options > 1 || count.totalCount == 0)
    {
        //
        [self performSegueWithIdentifier:@"multipleDetails" sender:self];

    }else
    {
        [self loadImage];
        Image *link = [_urlFetchedResultsController objectAtIndexPath:indexPath];
        webViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"webView"];
        detailViewController.webData = link.link;

        [self.navigationController pushViewController:detailViewController animated:YES];

   }

这是获取数据的部分。

- (void)loadMakeModel {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"MakeModel" inManagedObjectContext:localContext]];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(make == %@)",currentMake];
    [fetchRequest setPredicate: predicate];

    //NSLog(@"fetch request: %@",fetchRequest);
    [fetchRequest setFetchBatchSize:40];
    NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"Model" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortByName]];

    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:localContext sectionNameKeyPath:nil cacheName:nil];

    [_fetchedResultsController performFetch:nil];
}

- (void)loadOptions {
    [self loadMakeModel];

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    optionTitle = [_fetchedResultsController objectAtIndexPath:indexPath];
    NSString *options = [MakeModel MR_findFirstByAttribute:@"OptionTitle" withValue:optionTitle.makeTitle];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"FCOptions" inManagedObjectContext:localContext]];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(options == %@)",options];
    //
    [fetchRequest setPredicate: predicate];

    NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortByName]];

    _optionsFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:localContext sectionNameKeyPath:nil cacheName:nil];

    [_optionsFetchedResultsController performFetch:nil];


}
- (void)loadImage {
    [self loadOptions];

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    ImageUrl = [_optionsFetchedResultsController objectAtIndexPath:indexPath];
//    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"CarImage" inManagedObjectContext:localContext]];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(url == %@)",ImageUrl.url];
    //
    [fetchRequest setPredicate: predicate];
    NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"url" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortByName]];

    _urlFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:localContext sectionNameKeyPath:nil cacheName:nil];

    [_urlFetchedResultsController performFetch:nil];

}

问题是optionTitle由于某种原因是零。 任何帮助或示例将不胜感激。

1 个答案:

答案 0 :(得分:0)

好的,我通过使用:

来解决这个问题

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

而不是

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];