Ошибка при передаче данных между представлениями

Возможный дубликат:
Как передать данные в подробное представление после выбора в табличном представлении?

Я использую plist (массив словарей) для заполнения таблицы. Теперь, когда я нажал ячейку, я хочу передать обновленный словарь в подробное представление с переходом. Он работает правильно, чтобы заполнить табличное представление, но как отправить то же значение в подробное представление? Это моя попытка:

#import "WinesViewController.h"
#import "WineObject.h"
#import "WineCell.h"
#import "WinesDetailViewController.h"

@interface WinesViewController ()

@end

@implementation WinesViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (void)viewWillAppear:(BOOL)animated {
    wine = [[WineObject alloc] initWithLibraryName:@"Wine"];
    self.title = @"Vinene";
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}

- (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 [wine libraryCount];
}

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

    WineCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...


    cell.nameLabel.text = [[wine libraryItemAtIndex:indexPath.row] valueForKey:@"Name"];
    cell.districtLabel.text = [[wine libraryItemAtIndex:indexPath.row] valueForKey:@"District"];
    cell.countryLabel.text = [[wine libraryItemAtIndex:indexPath.row] valueForKey:@"Country"];
    cell.bottleImageView.image = [UIImage imageNamed:[[wine libraryItemAtIndex:indexPath.row] valueForKey:@"Image"]];
    cell.flagImageView.image = [UIImage imageNamed:[[wine libraryItemAtIndex:indexPath.row] valueForKey:@"Flag"]];
    cell.fyldeImageView.image = [UIImage imageNamed:[[wine libraryItemAtIndex:indexPath.row] valueForKey:@"Fylde"]];
    cell.friskhetImageView.image = [UIImage imageNamed:[[wine libraryItemAtIndex:indexPath.row] valueForKey:@"Friskhet"]];
    cell.garvesyreImageView.image = [UIImage imageNamed:[[wine libraryItemAtIndex:indexPath.row] valueForKey:@"Garvesyre"]];

    return cell;
}


#pragma mark - Table view delegate

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"DetailSegue"]) {

        NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
        WinesDetailViewController *winesdetailViewController = [segue destinationViewController];
        //Here comes the error line:
        winesdetailViewController.winedetailName = [wine objectAtIndex:indexPath.row] valueForKey:@"Name"];
    }
}

Я получаю одну ошибку для:

winesdetailViewController.winedetailName = [wine objectAtIndex:indexPath.row] valueForKey:@"Name"];

Использование необъявленного идентификатора: indexPath. Вы имели в виду NSIndexPath? Я думаю, что я пропустил что-то важное здесь..


person ingenspor    schedule 07.07.2012    source источник


Ответы (2)


Вместо indexPath.row (которого нет в этой функции, как в вашей функции tableView:cellForRowAtIndexPath:, где он передается в качестве аргумента) используйте selectedRowIndex следующим образом:

 winesdetailViewController.winedetailName = [wine objectAtIndex:selectedRowIndex.row] valueForKey:@"Name"];

Или, что еще лучше, измените контроллер подробного представления, чтобы он просто принимал весь словарь, передал словарь и позволил ему установить свои собственные значения:

 winesdetailViewController.winedetail = [wine objectAtIndex:selectedRowIndex.row];
person lnafziger    schedule 07.07.2012

Ошибка правильная, там не определена переменная indexPath. Однако всего парой строк выше этой строки вы определяете selectedRowIndex, который, вероятно, имеет значение, которое вам нужно.

person joshOfAllTrades    schedule 07.07.2012