2013-02-22 2 views
2

Im пытается сделать небольшой проект. Может ли кто-нибудь помочь мне понять, почему segue не работает?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; 
} 

Код отлично заводская, и загружает 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

См. Эти [Цель C] (http://stackoverflow.com/a/22761617/3681880) или [Быстрые ответы] (http://stackoverflow.com/a/31936367/3681880) – Suragch

ответ

4

В раскадровке вы использовали прототип клетки? Если да, то управляете ли вы перетаскиванием из прототипа в диспетчер представлений цели и создаете названный сегмент?

Метод, который вы используете, требует, чтобы segue полностью определялся в раскадровке, то есть триггер для segue привязан к прототипу ячейки.

Вы можете вручную вызвать SEGUE с помощью:

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

в ваш:

tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath в качестве альтернативы.

Смежные вопросы