显示Core Data的NSSet数据

时间:2013-07-09 23:58:56

标签: ios objective-c core-data nsarray nsset

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

enter image description here

我知道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>(entity:RoutinesDetails; id:0x74531f0   < X-coredata:// C075DDEC-169D-46EC-A4B7-972A04FCED70 / RoutinesDetails / P1>   ;数据:< fault>)“       )

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

**

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

**

routines.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;
}

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分: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);
}