【问题标题】:Pass data from plist based UITableView to UIViewController [duplicate]将数据从基于 plist 的 UITableView 传递到 UIViewController [重复]
【发布时间】:2012-06-24 08:06:25
【问题描述】:

可能重复:
How to pass data to detail view after being selected in a table view?

我有一个带有原型单元格的 TableViewController。它由带有字典数组的 .plist 中的标签和图像视图填充。如何将此数据传递给详细视图?详细视图是 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]。

标签: iphone class uitableview uiview uinavigationcontroller


【解决方案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"];
}

【讨论】:

  • 谢谢!我尝试在 viewDidAppear 中以这种方式访问​​字典: countryLabel.text = [wineDictionary objectForKey:@"Country"];制作出口后,但没有显示任何内容。当我尝试 NSString *country = [wineDictionary objectForKey:@"country"];它说那个国家是一个未使用的变量。关于如何设置的任何建议? (我是新手,所以需要一段时间才能得到它)
  • 它告诉国家是一个未使用的变量,因为它没有被使用。尝试像这样放置一个 NSLog NSString *country = [wineDictionary objectForKey:@"country"]; NSLog(@"国家 = %@", 国家);你在那里得到什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多