问题出在我的didSelectRowAtIndexPath
上,一旦我点击了调试区域出现的单元格,我不明白,我很抱歉,因为我现在只是初学者。
2014-05-08 12:59:41.118 MyWishlistV2[1334:60b] -[NSManagedObject wishlistItem]: unrecognized selector sent to instance 0x8e69c80
2014-05-08 12:59:41.121 MyWishlistV2[1334:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject wishlistItem]: unrecognized selector sent to instance 0x8e69c80'
我想要完成的是当我单击单元格时,它将转到另一个ViewController(DetailViewController),但我不知道为什么这不起作用。有人可以检查我在这里做错了什么吗?这是我的代码。
#import "WishTableViewController.h"
#import "WishlistItem.h"
#import "DetailViewController.h"
@interface WishTableViewController ()
@property (strong) NSMutableArray *wishes;
@end
@implementation WishTableViewController
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Fetch the wishes from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Wish"];
self.wishes = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return self.wishes.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *wish = [self.wishes objectAtIndex:indexPath.row];
//Set Wish
NSString *myWish = [wish valueForKey:@"wishItem"];
NSString *myTargetDate = [wish valueForKey:@"wishTargetDate"];
cell.textLabel.text = myWish;
cell.detailTextLabel.text = myTargetDate;
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[context deleteObject:[self.wishes objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
}
// Remove wish from table view
[self.wishes removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WishlistItem *wish = [self.wishes objectAtIndex:indexPath.row];
DetailViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"dvcID"];
dvc.wishItemStr = wish.wishlistItem;
dvc.dateItemStr = wish.targetDate;
dvc.descItemStr = wish.descWishItem;
[self.navigationController pushViewController:dvc animated:YES];
}
@end
答案 0 :(得分:1)
设置和使用NSFetchedResultsController非常重要,例如
MyObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];