UITableView选择Segue无法正常工作

时间:2014-03-18 15:30:39

标签: ios objective-c uitableview

我是iOS新手,我不明白为什么当我在单元格中点击我的项目时它无处可去。没有错误,没有任何错误。我的segue ID是Cell。我有一个可以正常添加数据的按钮,但是如果从表中选择的话,我试图更新数据。

这是我的.h.m

DerbyProTableViewController.h:

#import <UIKit/UIKit.h>

@interface DerbyProTableViewController : UITableViewController
@property (strong) NSMutableArray *racerarray;
@end

DerbyProTableViewController.m:

#import "DerbyProTableViewController.h"
#import "DerbyProDetailViewController.h"

@interface DerbyProTableViewController ()

@end

@implementation DerbyProTableViewController

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Racer"];
    self.racerarray = [[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
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
   return self.racerarray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    NSManagedObject *device = [self.racerarray objectAtIndex:indexPath.row];
    [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@", [device valueForKey:@"lastname"], [device valueForKey:@"firstname"]]];
    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) {
        // Delete object from database
        [context deleteObject:[self.racerarray objectAtIndex:indexPath.row]];

        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
            return;
        }

        // Remove device from table view
        [self.racerarray removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 if ([[segue identifier] isEqualToString:@"updateracer"]){
     NSManagedObject *selectedDevice = [self.racerarray objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
     DerbyProDetailViewController *destinationViewController = segue.destinationViewController;
     destinationViewController.derbyprodb = selectedDevice;
 }

 }

2 个答案:

答案 0 :(得分:3)

我正在接受您粘贴的代码中所看到的内容。我不知道故事板中可能设置了什么,但是......

没有为UITableView对象指定委托或数据源。 UITableViewDelegate处理单元格选择。 一旦你设置了这样的代表:

[self.tableview setDelegate:self];

然后您需要实施:

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

像这样:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self performSegueWithIdentifier:@"whateveryoursegueiscalled" sender:thecellyoutapped];
}

阅读Apple的tableview编程指南。

答案 1 :(得分:-2)

您可能无法在故事板中的“身份”检查器中添加课程。

在故事板中选择您的视图,然后单击identity ispector。确保Class字段不为空