Reputation: 183
There are similar questions on SO but I can't find the exact answer that will help me. I have a Core Data project with two entities, Exercises and Images. The app allows users to add exercises to a SQL database and displays them in a tableView. Attributes such as exercise name, description, and type are in the Exercises entity. Two images are in the Images entity. Everything works well except I want to have a thumbnail image of each exercise in each cell of the table. I can not figure out how to access one of the images from the Image entity. Here is the relevant code from tableView implementation file:
#import "TableViewController.h"
#import "AddNewExViewController.h"
#import "Exercises.h"
#import "Images.h"
#import "DetailViewController.h"
@interface TableViewController ()
@end
@implementation TableViewController
@synthesize context, arr;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
self.title = @"Exercises";
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Exercises" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setFetchBatchSize:20];
[request setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *newArray = [NSArray arrayWithObject:sort];
[request setSortDescriptors:newArray];
NSError *error;
NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy];
[self setArr:results];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Exercises *exercises = [arr objectAtIndex:indexPath.row];
cell.textLabel.text = [exercises name];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@, %@", exercises.type, exercises.part, exercises.difficulty];
cell.imageView.image = [UIImage imageWithData:[exercises valueForKey:@"image2"]];
return cell;
}
@end
The error occurs on the line cell.imageView.image = [UIImage imageWithData:[exercises valueForKey:@"image2"]]. I understand why but I don't know how to fix it.
Update
This problem is fixed now, thanks to Dan Shelly's suggestion:to use valueForKeyPath instead of valueForKey. I corrected this throughout the app. There are no errors HOWEVER the images are not being displayed in the tableView or cell or anywhere else in the app. I checked the SQL database and they are there. The user should be able to add images in the AddExViewController:
AddExViewController.m
- (void) add
{
NSManagedObjectContext *context = [app managedObjectContext];
Exercises *exercises = [NSEntityDescription insertNewObjectForEntityForName:@"Exercises" inManagedObjectContext:context];
// removed code not relevant for this issue
//convert images into NSData
NSData *img1 = [NSData dataWithData:UIImageJPEGRepresentation(imageView1.image, 0.5)];
[exercises.self setValue:img1 forKeyPath:@"imgs.image1"];
NSData *img2= [NSData dataWithData:UIImageJPEGRepresentation(imageView2.image, 0.5)];
[exercises.self setValue:img2 forKeyPath:@"imgs.image2"];
[self dismissViewControllerAnimated:YES completion:nil];
}
But the images do not show in the tableView cell, nor do they show in the DetalViewController which is called when the user taps on a row:
DetailViewController.m
- (void) viewWillAppear:(BOOL)animated
{
//convert images back from NSData back into UIImage
UIImage *image1 = [UIImage imageWithData:[exercises valueForKeyPath:@"imgs.image1"]];
[[self imageView1] setImage:image1];
UIImage *image2 = [UIImage imageWithData:[exercises valueForKeyPath:@"imgs.image2"]];
[[self imageView2] setImage:image2];
}
Again, everything worked fine when I only used one entity.
Upvotes: 0
Views: 414
Reputation: 6011
First, Apple recommend to put the images in separate files (not in CoreData store).
Second, since now your images are in their own entity, you should change your code to accessing the image data to something like:
[UIImage imageWithData:[obj valueForKeyPath:@"image2.imageData"]];
Also, I would advise to add this line to your fetch request so your UI will run smoother:
[request setRelationshipKeyPathsForPrefetching:@[@"image2"]];
And altogether, change your VC to use a NSFetchedResultsController
Upvotes: 1