저는 아직 IOS 개발에 초보자입니다. 내 ViewController
에서 개체를 제거 할 필요가 있지만, 엑스 코드는 fetchedResultsController를 알 수없는 말 : 사전에commitEditingStyle 내의 NSManagedObject를 제거하십시오.
#import "ViewController.h"
#import "CoreData/CoreData.h"
@interface ViewController()
@property (strong) NSMutableArray *devices;
@end
@implementation ViewController
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
...
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// two following lines probably are erroneous
NSManagedObject *entityToDelete =
[fetchedResultsController objectAtIndexPath:indexPath];
[mangedObjectContext deleteObject:entityToDelete];
// Remove the row from data model
[self.devices removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [device valueForKey:@"name"], [device valueForKey:@"version"]];
cell.detailTextLabel.text = [device valueForKey:@"company"];
}
감사합니다.
나는 코드를 변경했습니다. 이제는 "NSMutableArray가 'objectAtIndex'선택기를 선언한다는 표시가 없습니다. –
Ok - 실제로 fetchedResultsController가 있습니까? – pbasdf
아니요,하지 않았습니다. . –