从 plist 将数据推送到 DetailView Controller
Push Data to DetailView Controller from plist
我已经阅读了很多 post 仍然无法将数据从我的 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];
}
再次感谢您的帮助!!!
在我看来,你要传递的东西是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
语句,这样,如果事情没有按您预期的那样发生,您将看到您传递的数据的内容。)
我已经阅读了很多 post 仍然无法将数据从我的 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];
}
再次感谢您的帮助!!!
在我看来,你要传递的东西是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
语句,这样,如果事情没有按您预期的那样发生,您将看到您传递的数据的内容。)