简体   繁体   English

来自plist的数据未加载

[英]data from from plist did not load

Can anyone help me to solve the problem. 谁能帮我解决问题。 I would like to develop my app using property list with nsobject. 我想使用nsobject的属性列表来开发我的应用程序。 When run the app, there is no error but the data from bnm.plist did not load on uitableview. 运行该应用程序时,没有错误,但是bnm.plist中的数据未加载到uitableview上。

Below is ViewController.m 下面是ViewController.m

#import "MCWViewController.h"
#import "MCWPlacesDetailViewController.h"
#import "MCWPlaces.h"

@interface MCWViewController ()

@end

@implementation MCWViewController {
    NSArray *places;
}

@synthesize tableView = _tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Initialize table data

    // Find out the path of bnm.plist
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bnm" ofType:@"plist"];

    // Load the file content and read the data into arrays
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    MCWPlaces *placeGO = [MCWPlaces new];
    placeGO.name = [dict objectForKey:@"PlaceName"];
    placeGO.info = [dict objectForKey:@"PlaceInfo"];

    places = [NSArray arrayWithObject:placeGO];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [places count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"PlacesCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSLog(@"PlacesCell");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    MCWPlaces *place = [places objectAtIndex:indexPath.row];
    cell.textLabel.text = place.name;
    return cell;


}


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"showPlaceDetails"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        MCWPlacesDetailViewController *destViewController = segue.destinationViewController;
        destViewController.place = [places objectAtIndex:indexPath.row];
    }
}

Below is Places.h 以下是Places.h

#import <Foundation/Foundation.h>

@interface MCWPlaces : NSObject


@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *info;

@end

Below is Places.m 以下是Places.m

#import "MCWPlaces.h"

@implementation MCWPlaces

@synthesize name;
@synthesize info;


@end

Below is Details.m 以下是Details.m

#import "MCWPlacesDetailViewController.h"


@interface MCWPlacesDetailViewController ()

@end

@implementation MCWPlacesDetailViewController 

@synthesize placeInfo;
@synthesize place;

- (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.
    self.title = place.name;
    self.placeInfo.text = place.info;

}


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

@end

Below is Details.h 以下是Details.h

#import <UIKit/UIKit.h>
#import "MCWPlaces.h"

@interface MCWPlacesDetailViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *placeInfo;

@property (nonatomic, strong) MCWPlaces *place;



@end

bnm.plist bnm.plist

<plist version="1.0">
<dict>
    <key>PlaceName</key>
    <array>
        <string>Place 1</string>
        <string>Place 2</string>
        <string>Place 3</string>
    </array>
    <key>PlaceInfo</key>
    <array>
        <string>Place 1 Info</string>
        <string>Place 2 Info</string>
        <string>Place 3 Info</string>
    </array>
</dict>
</plist>

Did something wrong in my code? 我的代码有问题吗?

I think my problem is 'cellForRowAtIndexPath' and i have declared 'name' and 'info' as nsstring on Places.h. 我认为我的问题是“ cellForRowAtIndexPath”,我已经在Places.h上将“ name”和“ info”声明为nsstring。 but on plist is as nsarray. 但是在plist上就像nsarray一样。 but i dont know how to solve the problem. 但我不知道如何解决问题。

I think you have a problem when you load your data. 我认为您在加载数据时遇到了问题。

Firstly you say you get data from place.plist but in your viewDidLoad you tried to load data from bnm.plist 首先,您说您从place.plist获取数据,但是在您的viewDidLoad中,您尝试从bnm.plist加载数据

Try to use the following code : 尝试使用以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Initialize table data

    // Find out the path of bnm.plist
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bnm" ofType:@"plist"]];

    // Load the file content and read the data into arrays
    MCWPlaces *placeGO = [MCWPlaces new];
    placeGO.name = [dict objectForKey:@"PlaceName"];
    placeGO.info = [dict objectForKey:@"PlaceInfo"];
}

Could you show us your bnm.plist ? 您能告诉我们您的bnm.plist吗?

Ok so you are in the wrong way. 好的,所以您的方法有误。 Use the following code and replace it in your project : 使用以下代码并将其替换为您的项目:

bnm.plist bnm.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>PlaceName</key>
        <string>Place 1</string>
        <key>PlaceInfo</key>
        <string>Place 1 Info</string>
    </dict>
    <dict>
        <key>PlaceName</key>
        <string>Place 2</string>
        <key>PlaceInfo</key>
        <string>Place 2 Info</string>
    </dict>
</array>
</plist>

MCWPlaces.h MCWPlaces.h

@interface MCWPlaces : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *info;

@end

MCWPlaces.m MCWPlaces.m

#import "MCWPlaces.h"

@implementation MCWPlaces

@synthesize name;
@synthesize info;

@end

MCWViewController.m MCWViewController.m

#import "MCWViewController.h"
#import "MCWPlaces.h"

#define KEY_PLACE_NAME  @"PlaceName"
#define KEY_PLACE_INFO  @"PlaceInfo"

@interface MCWViewController ()

@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *places;

@end


@implementation MCWViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Init place array
    _places = [NSMutableArray array];

    // Find out the path of bnm.plist
    NSArray *data = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bnm" ofType:@"plist"]];

    NSLog(@"%@", data);

    for (NSInteger i = 0; i < data.count; i++)
    {
        MCWPlaces *placeGO = [MCWPlaces new];

        placeGO.name = [[data objectAtIndex:i] objectForKey:KEY_PLACE_NAME];
        placeGO.info = [[data objectAtIndex:i] objectForKey:KEY_PLACE_INFO];

        [_places addObject:placeGO];
    }

    NSLog(@"%@", _places);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _places.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"PlacesCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSLog(@"PlacesCell");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }



    NSLog(@"%@", [_places objectAtIndex:indexPath.row]);

    cell.textLabel.text = [[_places objectAtIndex:indexPath.row] name];
    return cell;


}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM