2012-12-29 85 views
0

我試圖弄清楚如何使用plist中的數據填充DVC中的文本域。我得到的數據填充到知道問題的tableview單元格中。我沒有發佈。 M代表DVC,因爲簡單地說。我不知道該怎麼加有...如何使用plist中的數據填充文本域

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 

爲talbleview

#import <UIKit/UIKit.h> 

@interface plistTableViewController : UITableViewController 
{ 
    NSArray *tabledata; 
} 

@end 

.M .H適用於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

使你的設計的一些假設 - 但我認爲要做到以下幾點:

爲模型實現的細節視圖控制器上的屬性您想要表示的對象。由於您使用的是內置的集合類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; 

然後在DetailViewControllerviewDidLoad方法,你可以設置的UI元素到任何條目在NSDictionaryself.contactInfo你的需要。

+0

工程就像一個魅力!謝謝!我會給你+10,如果我可以:) – Apps