0

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

我有一个带有原型单元的 TableViewController。它由带有字典数组的 .plist 中的标签和图像视图填充。如何将此数据传递给详细视图?详细视图是 UIViewController 的子类。我已经尝试过不同教程中的方法,但我找不到合适的组合来使它起作用。代码示例会很棒!

WinesViewController.h

#import <UIKit/UIKit.h>
@class WineObject;

@interface WinesViewController : UITableViewController {
    WineObject *wine;
}

@end

WinesViewController.m

- (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";

//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
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"]];

return cell;
}

wineobject.m

@implementation WineObject 
@synthesize libraryContent, libraryPlist;

- (id)initWithLibraryName:(NSString *)libraryName {
if (self = [super init]) {
    libraryPlist = libraryName;
    libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
                                                              pathForResource:libraryPlist ofType:@"plist"]];
}
return self;
}


- (NSDictionary *)libraryItemAtIndex:(int)index {
return (libraryContent != nil && [libraryContent count] > 0 && index < [libraryContent count]) 
? [libraryContent objectAtIndex:index]
: nil;
}

- (int)libraryCount {
return (libraryContent != nil) ? [libraryContent count] : 0;
}

- (void) dealloc {
if (libraryContent) [libraryContent release];
[super dealloc];
}

@end

WineCell.m

#import <UIKit/UIKit.h>

@interface WineCell : UITableViewCell

@property (nonatomic, strong) IBOutlet UILabel *nameLabel;
@property (nonatomic, strong) IBOutlet UILabel *districtLabel;
@property (nonatomic, strong) IBOutlet UILabel *countryLabel;

@end
4

1 回答 1

1

WinesViewController.m 您可以将字典传递给详细视图,不是吗?也许是这样的:

#import "WineDetailViewController.h"

@interface WinesViewController()
@property (nonatomic, retain) WineDetailViewController *wineDetailViewController;
@end

@implementation WinesViewController
@synthetize wineDetailViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];    

    // This will give us the wine dictionary
    NSDictionary *dictionary = [wine libraryItemAtIndex:indexPath.row];


    if (wineDetailViewController == nil) {
      // Init the wine detail view
      wineDetailViewController = [WineDetailViewController alloc] init];  
    }
    // Here you pass the dictionary
    wineDetailViewController.wineDictionary = dictionary;

    [self.navigationController pushViewController:wineDetailViewController animated:YES];
}

WineDetailViewController.h

#import <UIKit/UIKit.h>
@interface WineDetailViewController : UIViewController {
}
@property (nonatomic, retain) NSDictionary *wineDictionary;
@end

WineDetailViewController.m

@implementation WineDetailViewController
@synthetize wineDictionary;

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];
  // Here you access the wine dictionary
  NSString *country = [wineDictionary objectForKey:@"country"];
}
于 2012-06-22T07:23:32.027 回答