2013-02-22 80 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作爲替代。