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