2014-03-25 49 views
0

我正在与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是零。 任何帮助或例子,将不胜感激。

+2

什么是芯片问题是什么?可能你最好展示一些代码。 –

回答

0

好,我fiqured它使用:的

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

代替

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];