2013-12-12 16 views
-1

我不知道我在做什么错,但我的应用程序不按预期工作。我有一个tableview控制器,今天我已经包含didSelectRowAtIndexPath:方法来打开选定行的详细视图,但是当在该行上点击时什么都不会发生。这是我的代码,也许你会发现它没有触发细节视图的原因。我的应用程序不反应在didSelectRowAtIndex:方法

#import "RootViewController.h" 
#import "AddToDoViewController.h" 
#import "EditToDoViewController.h" 


@interface RootViewController() 
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath; 
@end 


@implementation RootViewController 

@synthesize fetchedResultsController, managedObjectContext,AddToDoButton; 


#pragma mark - 
#pragma mark View lifecycle 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self setTitle:@"Today"]; 
    [[self navigationItem] setRightBarButtonItem:[self editButtonItem]]; 





    NSError *error = nil; 
    if (![[self fetchedResultsController] performFetch:&error]) 
    { 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
    } 
} 
- (void) viewWillAppear:(BOOL)animated{ 
    NSError *error = nil; 
    if (![[self fetchedResultsController] performFetch:&error]) 
    { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 
    [self.tableView reloadData]; 
} 
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath]; 
    [[cell textLabel] setText:[[managedObject valueForKey:@"thingName"] description]]; 
    [[cell detailTextLabel] setText:[[managedObject valueForKey:@"thingDescription"] description]]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    cell.textLabel.textColor = [UIColor blueColor]; 
    cell.textLabel.font = [UIFont fontWithName:@"Noteworthy" size:17.0f]; 
    cell.detailTextLabel.font = [UIFont fontWithName:@"Noteworthy" size:15.0f]; 
} 

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


    EditToDoViewController *detailViewController = [[EditToDoViewController alloc] initWithNibName:@"EditToDoViewController" bundle:nil]; 
    NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 
    detailViewController.selectedObject = selectedObject; 

} 




#pragma mark - 
#pragma mark Table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [[fetchedResultsController sections] count]; 
} 

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

- (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:@"Cell"] autorelease]; 
    } 

    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext]; 
    [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]]; 

    NSError *error = nil; 
    if (![context save:&error]) 
    { 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 
    } 
} 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView 
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
     toIndexPath:(NSIndexPath *)destinationIndexPath; 
{ 
    NSMutableArray *things = [[fetchedResultsController fetchedObjects] mutableCopy]; 

    // Grab the item we're moving. 
    NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath]; 

    // Remove the object we're moving from the array. 
    [things removeObject:thing]; 
    // Now re-insert it at the destination. 
    [things insertObject:thing atIndex:[destinationIndexPath row]]; 

    // All of the objects are now in their correct order. Update each 
    // object's displayOrder field by iterating through the array. 
    int i = 0; 
    for (NSManagedObject *mo in things) 
    { 
    [mo setValue:[NSNumber numberWithInt:i++] forKey:@"displayOrder"]; 
    } 

    [things release], things = nil; 

    [managedObjectContext save:nil]; 
} 






#pragma mark - 
#pragma mark Fetched results controller 
- (IBAction)AddToDoAction:(id)sender { 

    AddToDoViewController *viewController = [[AddToDoViewController alloc] init]; 
    [self presentViewController:viewController animated:YES completion:nil]; 

} 

- (NSFetchedResultsController *)fetchedResultsController 
{ 
    if (fetchedResultsController) return fetchedResultsController; 

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

    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sortDescriptor = 
       [[NSSortDescriptor alloc] initWithKey:@"displayOrder" 
              ascending:YES]; 

    NSArray *sortDescriptors = [[NSArray alloc] 
           initWithObjects:sortDescriptor, nil]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 

    NSFetchedResultsController *aFetchedResultsController = 
       [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                managedObjectContext:managedObjectContext 
                sectionNameKeyPath:nil cacheName:@"ThingsCache"]; 
    aFetchedResultsController.delegate = self; 
    [self setFetchedResultsController:aFetchedResultsController]; 

    [aFetchedResultsController release]; 
    [fetchRequest release]; 
    [sortDescriptor release]; 
    [sortDescriptors release]; 

    return fetchedResultsController; 
}  

- (void)dealloc { 
    [fetchedResultsController release]; 
    [managedObjectContext release]; 


    [super dealloc]; 
} 


@end 

这里是我的头文件代码:

#import <UIKit/UIKit.h> 
#import <CoreData/CoreData.h> 

@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate> { 
    NSFetchedResultsController *fetchedResultsController; 
    NSManagedObjectContext *managedObjectContext; 
} 
@property (retain, nonatomic) IBOutlet UIBarButtonItem *AddToDoButton; 
- (IBAction)AddToDoAction:(id)sender; 

@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController; 
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; 

@end 

回答

3
EditToDoViewController *detailViewController = [[EditToDoViewController alloc] initWithNibName:@"EditToDoViewController" bundle:nil]; 
NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 
detailViewController.selectedObject = selectedObject; 
[self.navigationController pushViewController:detailViewController animated:YES]; 

你忘了把你的视图控制器。

+0

@mvasco这看起来像缺少什么。直到您尝试添加此项时,请忽略我的建议。还要添加一个NSLog()来查看didSelect方法是否被调用。 –

+0

谢谢@johnMa和Duncan Groenewald,johnMa的建议解决方案使应用程序崩溃...警告问题:'实例方法:_pushViewController.animate未找到... – mvasco

+0

您确定已将您的'UIViewController'嵌入一个'UINavigationController'? – msgambel

相关问题