任何人都可以帮我解决问题。我想使用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一样。但我不知道如何解决这个问题。
答案 0 :(得分: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吗?
答案 1 :(得分: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