-1

可能重复:
在表格视图中选择后如何将数据传递到详细视图?

我正在使用 plist(字典数组)来填充表格视图。现在,当我按下一个单元格时,我想通过 segue 将 reprecented 字典传递给详细视图。填充表格视图可以正常工作,但是如何将相同的值发送到详细视图?这是我的尝试:

#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 吗?我想我在这里错过了一些重要的事情..

4

2 回答 2

1

而不是indexPath.row(它在这个函数中不存在,就像它在tableView:cellForRowAtIndexPath:作为参数传递的函数中一样)使用selectedRowIndex如下:

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

或者,更好的是,将您的详细视图控制器更改为只接受整个字典,传递字典,并让它设置自己的值:

 winesdetailViewController.winedetail = [wine objectAtIndex:selectedRowIndex.row];
于 2012-07-07T03:06:14.367 回答
1

错误是正确的,那里没有indexPath定义变量。但是,仅在您定义的那条线上方几行selectedRowIndex可能具有您所追求的值。

于 2012-07-07T03:07:01.890 回答