0

我试图弄清楚如何用 plist 中的数据填充 DVC 中的文本字段。我已经将数据填充到已知问题的 tableview 单元格中。我还没有发布。M 为 DVC,因为简单地说。我不知道要在那里添加什么...

xml / plist

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>cellName</key>
        <string>Sharon Lodge No. 327</string>
        <key>cellSubtitle</key>
        <string>McLean, VA</string>
        <key>address</key>
        <string>999 Balls Hill Road</string>
        <key>webSite</key>
        <string>www.website.com</string>
        <key>statedCom</key>
        <string>Hold meetins on...</string>
    </dict>
    <dict>
        <key>cellName</key>
        <string>Sharon Lodge No. 327</string>
        <key>cellSubtitle</key>
        <string>McLean, VA</string>
        <key>address</key>
        <string>999 Balls Hill Road</string>
        <key>webSite</key>
        <string>www.website.com</string>
        <key>statedCom</key>
        <string>Hold meetins on...</string>
    </dict>

.h 用于 DVC

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController


@property (strong, nonatomic) IBOutlet UILabel *address;
@property (strong, nonatomic) IBOutlet UILabel *webSite;
@property (strong, nonatomic) IBOutlet UILabel *statedCom;
@property (nonatomic, copy) NSDictionary *contactInfo; // added this 

@end

.h 用于桌面视图

#import <UIKit/UIKit.h>

@interface plistTableViewController : UITableViewController
{
    NSArray *tabledata;
}

@end

.M 表视图

#import "plistTableViewController.h"
#import "DetailViewController.h"
@interface plistTableViewController ()

@end

@implementation plistTableViewController


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    NSString *mylist = [[NSBundle mainBundle] pathForResource:@"lodges" ofType:@"plist"];
    tabledata = [[NSArray alloc]initWithContentsOfFile:mylist];
    NSLog(@"%@", tabledata);
    [super viewDidLoad];

   }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (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 [tabledata count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if( cell == nil )
    {
        NSLog(@"Cell Creation");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    //Set Data For each Cell
    cell.textLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellName"];
    cell.detailTextLabel.text = [[tabledata objectAtIndex:indexPath.row]objectForKey:@"cellSubtitle"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}



-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

添加了这个 NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow]; NSDictionary *contactInfo = tabledata[indexPath.row]; DetailViewController *dvc = (DetailViewController *)[segue destinationViewController]; dvc.contactInfo = 联系人信息;dvc.address = [[tabledata objectAtIndex:@"address"]]; // 这是错误的,但不确定如何正确编写。

}

.m 到 dvc

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

@synthesize address,webSite,statedCom;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    self.address =[[tabledata objectAtIndex:address]]; // not sure how to complete this



    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

1 回答 1

1

对您的设计做出一些假设 - 但我认为您想要执行以下操作:

在要表示的模型对象的详细视图控制器上实现一个属性。由于您使用的是内置集合类NSArray并且NSDictionary直接来自属性列表,我怀疑详细视图控制器上的属性将类似于:@property (nonatomic, copy) NSDictionary
*contactInfo;

在方法中的详细视图控制器上设置此属性 prepareForSegue:sender:。您需要从选定的索引路径中获取适当的字典,如下所示:

NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
NSDictionary *contactInfo = tabledata[indexPath.row];
DetailViewController *dvc = (DetailViewController *)[segue destinationViewController];
dvc.contactInfo = contactInfo;

然后在该方法中,您可以将 UI 元素设置为您需要DetailViewController viewDidLoad的任何条目。NSDictionary self.contactInfo

于 2012-12-30T04:43:00.970 回答