0

谁能帮我解决这个问题。我想使用带有 nsobject 的属性列表来开发我的应用程序。运行应用程序时,没有错误,但 bnm.plist 中的数据未加载到 uitableview。

下面是 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];
    }
}

下面是 Places.h

#import <Foundation/Foundation.h>

@interface MCWPlaces : NSObject


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

@end

下面是 Places.m

#import "MCWPlaces.h"

@implementation MCWPlaces

@synthesize name;
@synthesize info;


@end

以下是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

下面是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

<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>

我的代码有问题吗?

我认为我的问题是“cellForRowAtIndexPath”,我已在 Places.h 上将“name”和“info”声明为 nsstring。但在 plist 上是 nsarray。但我不知道如何解决这个问题。

4

2 回答 2

0

好的,所以你走错路了。使用以下代码并在您的项目中替换它:

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

@interface MCWPlaces : NSObject

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

@end

MCWPlaces.m

#import "MCWPlaces.h"

@implementation MCWPlaces

@synthesize name;
@synthesize info;

@end

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
于 2013-09-22T10:34:56.577 回答
0

我认为您在加载数据时遇到了问题。

首先,您说您从 place.plist 获取数据,但在您的 viewDidLoad 中,您尝试从 bnm.plist 加载数据

尝试使用以下代码:

- (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"];
}

你能告诉我们你的 bnm.plist 吗?

于 2013-09-21T22:49:45.543 回答