将数据从plist基于UITableView传递到UIViewController [英] Pass data from plist based UITableView to UIViewController

查看:135
本文介绍了将数据从plist基于UITableView传递到UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何在表视图中选择后将数据传递到详细视图?





我有一个TableViewController与protoptype单元格。它用来自.plist的Labels和ImageViews填充了一个字典数组。如何将此数据传递到详细视图?详细视图是一个UIViewController子类。
我试过不同教程的方法,但我只是找不到正确的组合,使其工作。代码示例非常棒!

I have a TableViewController with a protoptype cell. It's populated with Labels and ImageViews from a .plist with an Array of Dictionaries. How can I pass this data to the detail View? The detail view is a UIViewController subclass. I've tried with methods from different tutorials but I just can't find the right combination to make it work. Code examples would be great!

WinesViewController.h

WinesViewController.h

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

@interface WinesViewController : UITableViewController {
    WineObject *wine;
}

@end

WinesViewController.m

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

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

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


推荐答案

WinesViewController .m
你可以将字典传递给详细视图,不是?可能是这样的:

WinesViewController.m You can just pass the dictionary to the detail view, no ? Maybe something like this:

#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

WineDetailViewController.h

#import <UIKit/UIKit.h>
@interface WineDetailViewController : UIViewController {
}
@property (nonatomic, retain) NSDictionary *wineDictionary;
@end

WineDetailViewController.m

WineDetailViewController.m

@implementation WineDetailViewController
@synthetize wineDictionary;

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];
  // Here you access the wine dictionary
  NSString *country = [wineDictionary objectForKey:@"country"];
}

这篇关于将数据从plist基于UITableView传递到UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆