2013-02-22 52 views
2

我试图做一个小项目。有人能帮我理解为什么赛格不起作用吗?UITableView Segue

#import "CarTableViewController.h" 
#import "CarTableViewCell.h" 
#import "CarDetailViewController.h" 

@interface CarTableViewController() 




@end 

@implementation CarTableViewController 

@synthesize carMakes = _carMakes; 
@synthesize carModels = _carModels; 
@synthesize carImages = _carImages; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.carMakes = [[NSArray alloc] 
        initWithObjects:@"Chevy", 
        @"BMW", 
        @"Toyota", 
        @"Volvo", 
        @"Smart", nil]; 

    self.carModels = [[NSArray alloc] 
         initWithObjects:@"Volt", 
         @"Mini", 
         @"Venza", 
         @"S60", 
         @"Fortwo", nil]; 

    self.carImages = [[NSArray alloc] 
         initWithObjects:@"chevy_volt.jpg", 
         @"mini_clubman.jpg", 
         @"toyota_venza.jpg", 
         @"volvo_s60.jpg", 
         @"smart_fortwo.jpg", nil]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (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 _carModels.count; 
} 



#pragma mark - Table view data source 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"carTableCell"; 

    CarTableViewCell *cell = [tableView 
           dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[CarTableViewCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    cell.makeLabel.text = [self.carMakes 
          objectAtIndex: [indexPath row]]; 

    cell.modelLabel.text = [self.carModels 
          objectAtIndex:[indexPath row]]; 

    UIImage *carPhoto = [UIImage imageNamed: 
         [self.carImages objectAtIndex: [indexPath row]]]; 

    cell.carImage.image = carPhoto; 

    return cell; 
} 

代码work's罚款,并加载的tableView,但我需要去CarDetailViewControler和I'm使用下面的代码,但它不工作。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"ShowCarDetails"]) 
    { 
     CarDetailViewController *detailViewController = 
     [segue destinationViewController]; 

     NSIndexPath *myIndexPath = [self.tableView 
            indexPathForSelectedRow]; 

     detailViewController.carDetailModel = [[NSArray alloc] 
               initWithObjects: [self.carMakes 
                   objectAtIndex:[myIndexPath row]], 
               [self.carModels objectAtIndex:[myIndexPath row]], 
               [self.carImages objectAtIndex:[myIndexPath row]], 
               nil]; 
    } 
} 
+0

请参阅这些[Objective C](http://stackoverflow.com/a/22761617/3681880)或[Swift answers](http://stackoverflow.com/a/31936367/3681880) – Suragch 2015-08-11 08:12:50

回答

4

在故事板中,您是否使用了细胞原型?如果是这样,你是否控制 - 从原型拖动到目标视图控制器并创建指定的segue?

您正在使用的方法要求将segue完全定义在故事板中,这意味着segue的触发器与细胞原型绑定。

可以使用手动触发赛格瑞:

[self performSegueWithIdentifier:@"ShowCarDetails" sender:self]; 

tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath作为替代。