2012-06-22 55 views
0

可能重複:
How to pass data to detail view after being selected in a table view?將數據傳遞給UIViewController的

我有一個protoptype細胞TableViewController。它使用字典數組填充來自.plist的Labels和ImageViews。我如何將這些數據傳遞給詳細視圖?詳細視圖是一個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 
+1

你有沒有的tableView的DataSource屬性設置?你可以在IB/storyboard中做到這一點,或者你可以在代碼中做到這一點...雖然我沒有看到它在這個代碼中。如果沒有設置,那麼tableView:cellForRowAtIndexPath:永遠不會被調用。另一種可能性是,在viewWillAppear中,您可能必須在wine對象從庫中填充後調用[self.tableView reloadData]。 –

回答

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"]; 
} 
+0

謝謝!我試圖在viewDidAppear中以這種方式訪問​​字典:countryLabel.text = [wineDictionary objectForKey:@「Country」];在製作出口後,但沒有顯示任何東西。當我嘗試NSString * country = [wineDictionary objectForKey:@「country」];這是國家是一個未使用的可變物。有關如何設置它的任何建議? (我是新手,所以需要一段時間才能得到它) – ingenspor

+0

這是說國家是一個未使用的變量,因爲它沒有被使用。嘗試把這樣一個NSLog NSString * country = [wineDictionary objectForKey:@「country」]; NSLog(@「country =%@」,country); 你到那裏去了什麼? – ewiinnnnn