谁能帮我解决这个问题。我想使用带有 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。但我不知道如何解决这个问题。