从plist将数据推送到DetailView控制器

时间:2015-01-08 13:02:18

标签: ios objective-c

我读过许多帖子仍然无法从我的plist

将数据推送到我的DetailViewController

我尝试在模拟器上运行,显示CUICatalog:提供的资产名称无效:( null)

这在我的CustomTableViewController.m

上完成
#import "CustomTableViewController.h"
#import "customCell.h"
#import "DetailViewController.h"

@interface CustomTableViewController ()<UISearchDisplayDelegate, UISearchBarDelegate>


@property (nonatomic, strong)NSArray *contactsArray;
@property (nonatomic, strong)NSDictionary *contact;


@end

@implementation CustomTableViewController

@synthesize contactsArray, contact;



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

- (void)viewDidLoad
{
[super viewDidLoad];


NSString *path = [[NSBundle mainBundle]pathForResource:@"contacts" ofType:@"plist"];

contactsArray = [NSArray arrayWithContentsOfFile:path];

}



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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [contactsArray count];
 }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath        *)indexPath {

static NSString *CellIdentifier = @"cell";
customCell *customCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier       forIndexPath:indexPath];

// Configure the cell...

contact = contactsArray[indexPath.row];

NSString *firstName = contact[@"firstName"];
NSString *lastName = contact[@"lastName"];
NSString *imageName = contact[@"imageName"];
NSString *cost = contact[@"cost"];
NSString *property = contact[@"property"];

UIImage *image = [UIImage imageNamed:imageName];

customCell.customFirstNameLabel.text = firstName;
customCell.customLastNameLabel.text = lastName;
customCell.customImageView.image = image;
customCell.customcostLabel.text = cost;
customCell.custompropertyLabel.text = property;

return customCell;
}


-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath *)indexPath
{
 cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Card Background.png"]];
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}

希望有人可以帮助!!!!非常感谢

这是我的plist的一部分

     <dict>
    <key>firstName</key>
    <string>【騎士】聖夜型トール</string>
    <key>lastName</key>
    <string>傭兵</string>
    <key>imageName</key>
    <string>【騎士】聖夜型トール.png</string>
    <key>property</key>
    <string>闇</string>
    <key>rare</key>
    <string>UR</string>
    <key>cost</key>
    <string>5</string>
    <key>skillName</key>
    <string>制裁剣=討伐絆人(ホーリーデストロイ)</string>
    <key>normalSkill</key>
    <string>敵単体物理6,541闇ダメージ</string>
    <key>awrokenSkill</key>
    <string>敵単体物理10,851闇ダメージ、チェイン数で威力アップ</string>
    <key>hp</key>
    <string>1385</string>
    <key>pa</key>
    <string>231</string>
    <key>ma</key>
    <string>0</string>
    <key>heal</key>
    <string>0</string>
    </dict>

由于我在CustomeTableViewController中选择了行,我需要将所有内容全部推送到DetailViewController,现在什么都没有出现

我的DetailViewController.h

@interface DetailViewController : UIViewController{
IBOutlet UIScrollView *scroller;

}
@property (strong, nonatomic) IBOutlet UIImageView *imageName;
@property (strong, nonatomic) IBOutlet UILabel *firstName;
@property (strong, nonatomic) IBOutlet UILabel *lastName;
@property (strong, nonatomic) IBOutlet UILabel *property;
@property (strong, nonatomic) IBOutlet UILabel *rare;
@property (strong, nonatomic) IBOutlet UILabel *cost;
@property (strong, nonatomic) IBOutlet UILabel *hp;
@property (strong, nonatomic) IBOutlet UILabel *pa;
@property (strong, nonatomic) IBOutlet UILabel *heal;
@property (strong, nonatomic) IBOutlet UILabel *ma;
@property (strong, nonatomic) IBOutlet UILabel *skillName;
@property (strong, nonatomic) IBOutlet UILabel *normalSkill;
@property (strong, nonatomic) IBOutlet UILabel *awrokenSkill;


@property int selectedRow;

@end

然后是DetailViewController.m,我已经完成了这个

@implementation DetailViewController

@synthesize imageName;
@synthesize firstName;
@synthesize lastName;
@synthesize property;
@synthesize rare;
@synthesize cost;
@synthesize hp;
@synthesize pa;
@synthesize heal;
@synthesize ma;
@synthesize skillName;
@synthesize normalSkill;
@synthesize awrokenSkill;


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


- (void)viewDidLoad {
  [super viewDidLoad];
// Do any additional setup after loading the view.
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(600, 800)];

NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle     mainBundle] pathForResource:@"contacts" ofType:@"plist"]];
NSArray *imageArray = [picturesDictionary objectForKey:@"imageName"];
imageName.image = [UIImage imageNamed:[imageArray objectAtIndex:self.selectedRow]];



}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

再次感谢任何帮助!!!

1 个答案:

答案 0 :(得分:0)

在我看来,您要传递的内容是contactsArray[indexPath.row],即NSDictionary。 (我这样说是因为我假设NSString *firstName = contact[@"firstName"];正常工作。)

为此,您需要在详细信息视图控制器中使用NSDictionary属性,而不是selectedRow。类似的东西:

@property (nonatomic, strong) NSDictionary *picturesDictionary;

然后你将创建一个类似于你所拥有的prepareForSegue:方法,除了你将contactsArray[indexPath.row]字典传递给目标控制器。

在详情视图控制器的viewDidLoad中,您应该可以使用...

获取图像的路径
NSString *imgName = picturesDictionary[@"imageName"];

...而不是重新加载已在第一个控制器中加载的plist。

(我建议经常使用NSLog语句,这样,如果事情没有按照您的预期发生,您就会看到您传递的数据内容。)< / p>