可能重复:
How to pass data to detail view after being selected in a table view?
我正在使用plist(字典数组)来填充tableview。现在,当我按下一个单元格时,我想将带有重新设计的字典传递给带有segue的详细视图。它正常工作以填充tableview,但如何将相同的值发送到详细信息视图?这是我的尝试:
#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?我想我错过了一些重要的事情......
答案 0 :(得分:1)
而不是indexPath.row
(在此函数中不存在,就像它在作为参数传递的tableView:cellForRowAtIndexPath:
函数中一样)使用selectedRowIndex
,如下所示:
winesdetailViewController.winedetailName = [wine objectAtIndex:selectedRowIndex.row] valueForKey:@"Name"];
或者,更好的是,将您的详细视图控制器更改为只接受整个字典,传递字典,并让它设置其自己的值:
winesdetailViewController.winedetail = [wine objectAtIndex:selectedRowIndex.row];
答案 1 :(得分:1)
错误是正确的,那里没有定义变量indexPath
。但是,在该行之上只有几行,您可以定义selectedRowIndex
,这可能具有您所追求的价值。