DVCのテキストフィールドにplistのデータを入力する方法を見つけようとしています。問題がわかっているテーブルビューのセルにデータを入力しました。を投稿していません。簡単に言えば、DVC の M です。そこに何を追加すればいいのかわからない...
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 = contactInfo; dvc.address = [[tabledata objectAtIndex:@"アドレス"]]; // これは間違っていますが、正しい書き方がわかりません。
}
.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