如何使用plist中的数据填充文本字段

时间:2012-12-29 23:27:24

标签: objective-c xml plist tableview

我试图弄清楚如何使用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 for 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 for talbleview

#import <UIKit/UIKit.h>

@interface plistTableViewController : UITableViewController
{
    NSArray *tabledata;
}

@end

.M for Tableview

#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 = 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

1 个答案:

答案 0 :(得分:1)

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

在详细视图控制器上为要表示的模型对象实现属性。由于您直接从属性列表中使用内置集合类NSArrayNSDictionary,我怀疑详细视图控制器上的属性将类似于:@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;

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