2012-06-26 5 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를 사용하거나 프로그래밍 방식으로 생성하고 있습니까? 당신은 그것을 당신이 XIB를 사용하는 경우


, 문제가를로드되지 않습니다 :

변경

winesDetailViewController = [[WinesDetailViewController alloc] init];

으로

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


프로그래밍 방식으로 생성하는 경우 먼저 nameLabel을 설정해야합니다. 그렇지 않으면 nil이됩니다. @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