2012-06-26 73 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

相關問題