2012-06-26 35 views
0

我有一个plist(字典数组)填充表视图并正常工作。我使用Xcode 4与故事板。从plist传递数据到详细视图

现在我已经创建了一个普通UIViewController的细节视图,当然我希望所选的名称在详细视图的nameLabel中显示。但我无法建立正确的联系。这是到目前为止我的代码:

WineObject.m:

#import "WineObject.h" 

@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 

ViewController.h:

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

@interface WinesViewController : UITableViewController { 
WineObject *wine; 
} 


@end 

ViewController.m:

#import "WinesViewController.h" 
#import "WineObject.h" 
#import "WineCell.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]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

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

@end 

WineCell.h:

#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; 
@property (nonatomic, strong) IBOutlet UIImageView *bottleImageView; 
@property (nonatomic, strong) IBOutlet UIImageView *flagImageView; 
@property (nonatomic, strong) IBOutlet UIImageView *fyldeImageView; 
@property (nonatomic, strong) IBOutlet UIImageView *friskhetImageView; 
@property (nonatomic, strong) IBOutlet UIImageView *garvesyreImageView; 

@end 

回答

1

您是使用XIB接口还是以编程方式生成它?


如果您使用的是厦门国际银行,问题是,你是不是装起来:

变化

winesDetailViewController = [[WinesDetailViewController alloc] init];

winesDetailViewController = [[WinesDetailViewController alloc] initWithNibName:@"YourNibNameHere" bundle:nil];


或者,如果以编程方式生成它,则必须先设置nameLabel,否则它将为零。 @synthesize不会设置变量,它只是生成getter和setter,以便您可以从外部进行设置。

里面你viewDidAppear:(或您的init内部更好的)加:

self.nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(100,100,100,100)];


编辑:如果您使用的是故事板,看来,你必须做到以下几点。

故事板都是关系关系。在故事板编辑器中,添加按钮并告诉它们连接到哪个视图控制器。同样的想法适用于TableView单元格。您可以添加原型表格视图单元格(并对其进行自定义)并为其分配关系。你想要给它的关系是你的细节视图。

1)子类的UITableViewCell,并给它一个属性,即你要发送到详细信息视图

2)在创建细胞(cellForRowAtIndexPath:),你将需要确保出队的字典您自定义单元格并将您的字典分配给您提供的属性。

3。)请确保您的详细信息视图的标识符为:DetailView

4)里面的表视图控制器,添加以下代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"DetailView"]) 
    { 
     //This works because by the time prepareForSeque: is called, the navigationController has loaded the new view 
     ((DetailView *)[[self.navigationController viewControllers] objectAtIndex:0]).dataProperty=((MyCustomTableViewCell *)sender).dataProperty; 
    } 
} 

这应该做到这一点!

+0

忘记告诉我,我用故事板Xcode 4! – ingenspor

+0

这有什么区别吗?不太熟悉故事板。 – teh1

+0

不,我不以编程方式生成它。检查我的编辑! – ingenspor

相关问题