将 Plist 填充到 UITableview

Populate Plist to UITableview

我的代码有问题,我不知道哪里出了问题。

我的代码是这样的:

@implementation FirstViewController {
NSDictionary *countriesDetails;
NSArray *countriesList;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"countries" withExtension:@"plist"];
    countriesDetails = [NSDictionary dictionaryWithContentsOfURL:url];
    countriesList = countriesDetails.allKeys;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = [[countriesList objectAtIndex:indexPath.row] objectForKey:@"country"];

    return cell;
}

我的 plist 文件是这样的:

<dict>
<key>Paris</key>
<dict>
    <key>country</key>
    <string>France</string>
    <key>chName</key>
    <string>Paris</string>
    <key>chlink</key>
    <string>www.paris.com</string>
</dict>
<key>Rome</key>
<dict>
    <key>country</key>
    <string>Italy</string>
    <key>chName</key>
    <string>Rome</string>
    <key>chlink</key>
    <string>www.rome.com</string>
</dict>

所以我的应用程序将有多个表格视图,第一个显示我的 plist 上的所有国家/地区。

我的 XCode 没有显示任何错误,但是当我 运行 应用程序崩溃并出现以下内容时:

2015-05-26 23:34:53.362 newApp[1511:95592] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fde31f242d0 2015-05-26 23:34:53.366 neaApp[1511:95592] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7fde31f242d0' * First throw call stack:

您的 countriesListNSString 组成,而不是 NSDictionary。 将您的 tableView:cellForRowAtIndexPath: 方法更改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = [[countriesDetails objectForKey:[countriesList objectAtIndex:indexPath.row]] objectForKey:@"country"];

    return cell;
}