【问题标题】:How do I populate a textfield with data from a plist如何使用 plist 中的数据填充文本字段
【发布时间】:2012-12-29 23:27:23
【问题描述】:

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

#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

【问题讨论】:

    标签: objective-c xml plist tableview


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

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

    【讨论】:

    • 像魅力一样工作!谢谢!如果可以的话,我会给你 +10 :)
    猜你喜欢
    • 2021-11-10
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多