2

我将值添加到我的核心数据模型中的 2 个实体中,我面临的问题是在访问详细视图时尝试正确检索 NSSet 并定位关联的字符串。我只想在 uitableview 中显示结果。我相信这些值与与实体的关系一起被正确添加,因为我之前从论坛获得了帮助,因为它可以在此处查看。我正在尝试在详细视图上调用相关的RoutinesDetail数据。

在此处输入图像描述

我知道 seague 工作正常,因为我能够根据 selectedRow 设置标题,因此它在视图控制器之间传递数据。我可以使用来回忆“例程”数据Ex.routinename

使用以下调试并尝试分配 NSSet,因为我阅读了一种显示它的方法,allObjects但这不起作用:

NSArray *test= [Ex.routinedet allObjects];
NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:@"image"
                                                     ascending:YES];
test = [test sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];

NSLog(@"Image *** %@",test);

这会产生以下报告,这让我质疑我是否做得对?

“<RoutinesDetails:0x7453d80>(实体:RoutinesDetails;id:0x74531f0 <x-coredata://C075DDEC-169D-46EC-A4B7-972A04FCED70/RoutinesDetails/p1>;数据:<fault>)”)

所以基本上我正在研究如何检索RoutinesDetails。任何建议将不胜感激。

**

如果需要这里是相关的代码

**

例程.h - NSManagedObject

@class RoutinesDetails;

@interface Routines : NSManagedObject

@property (nonatomic, retain) id routinename;
@property (nonatomic, retain) NSSet *routinedet;

@end

@interface Routines (CoreDataGeneratedAccessors)

- (void)addRoutinedetObject:(RoutinesDetails *)value;
- (void)removeRoutinedetObject:(RoutinesDetails *)value;
- (void)addRoutinedet:(NSSet *)values;
- (void)removeRoutinedet:(NSSet *)values;

@end

RoutinesDetails.h - NSManagedObject

@class Routines;

@interface RoutinesDetails : NSManagedObject

@property (nonatomic, retain) NSString * image;
@property (nonatomic, retain) NSString * name;

@property (nonatomic, retain) Routines *routineinfo;

@end

FBCDRoutineDetailViewController(对不起,如果它的代码太多)

#import "FBCDRoutineDetailViewController.h"
#import "FBCDRoutineViewController.h"
#import "Routines.h"
#import "RoutinesDetails.h"

@interface FBCDRoutineDetailViewController ()

@end

@implementation FBCDRoutineDetailViewController
@synthesize managedObjectContext;
@synthesize fetchedResultsController = _fetchedResultsController;
@synthesize Ex;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (NSFetchedResultsController *)fetchedResultsController {

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"RoutinesDetails" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"name" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:managedObjectContext sectionNameKeyPath:nil
                                                   cacheName: nil];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}

- (void)viewDidLoad
{
    [[self navigationController] setNavigationBarHidden:NO animated:YES];

    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSArray *test= [Ex.routinedet allObjects];
    NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:@"image"
                                                      ascending:YES];
    test = [test sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];

    NSLog(@"Image *** %@",test);

    self.title = Ex.routinename;

    NSLog(@"Image *** %@",Ex.routinename);

    }


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view delegate

- (IBAction)save:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)cancel:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];

}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{    }

#pragma mark - Table view delegate

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id  sectionInfo =
    [[_fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

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

    //RoutinesDetails *info = [_fetchedResultsController objectAtIndexPath:indexPath];

    NSArray *test= [Ex.routinedet allObjects];
    NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:@"image"
                                                         ascending:YES];

    test = [test sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];

    //cell.textLabel.text = info.image;

    cell.textLabel.text = [[test objectAtIndex:indexPath.row] name];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"routineCell";

    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Set up the cell...
    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

任何建议将不胜感激。

4

1 回答 1

3

我想你看到了你想要的。我假设 EX 是一个练习,一个例行程序,并且您正在尝试打印出相关的详细信息。您的调试语句显示一个 RoutinesDetails (entity: RoutinesDetails; id: 0x74531f0 ; data: )" )

试试这个来帮助你形象化

for (RoutinesDetails *detail in Ex.routinedet) {
  NSLog(@"image: %@, name: %@, routineName: %@",detail.image, detail.name, detail.routineinfo.routinename);
}
于 2013-07-10T00:38:13.613 回答