无法使用.plist填充UITableView [英] Cannot Feed UITableView with .plist

查看:87
本文介绍了无法使用.plist填充UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,可能有一个非常简单的问题.我浏览了类似的主题,却迷失了一天,没有找到解决方案. 我有一个Master和Detail视图控制器.我创建了一个.plist,如下所示.

I am a beginner and have a probably quite simple question. I browsed through similar topics and lost a day without finding a solution. I have a Master and Detail view controllers. I created a .plist as shown below.

现在,我想用这些数据输入UITableView.问题就在这里.我不能拿大洲的名字.一直以来,我看到空白单元格,或者出现一些错误.也许我应该更改plist中的某些内容?

Now I would like feed UITableView with these data. And here the problem is. I can't take continents names. All the time I see blank cells or I get some errors. Maybe I should change something in the plist?

这是我的代码: MasterViewController.h:

Here is my code: MasterViewController.h:

@interface MasterViewController : UITableViewController

@property (nonatomic, strong) NSDictionary *world;
@property (nonatomic, strong) NSDictionary *africa;
@property (nonatomic, strong) NSDictionary *europe;

@end

MasterViewController.m(我添加了东西的地方):

MasterViewController.m (the places where I added something):

@synthesize world, africa, europe;

- (void)viewDidLoad
{
self.navigationItem.title = @"World Info";
NSString *worldLibraryFile = [[NSBundle mainBundle] pathForResource:@"World" ofType:@"plist"];
world = [[NSDictionary alloc] initWithContentsOfFile:worldLibraryFile];
africa = [world objectForKey:@"Africa"];
europe = [world objectForKey:@"Europe"];

[super viewDidLoad];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [world count]; //this works fine
}

这是一个问题:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];


NSString *ContinentName = [world objectForKey:indexPath.row]; //I do something wrong here
cell.textLabel.text = ContinentName;

return cell;
}

如何在表格视图中显示我的大洲?如何获得国家/地区,然后在其他点击后显示其他信息?

How can I get my continents shown in the table view? How to get countries and then other info shown after another clicks?

谢谢您的帮助.

在进行以下更改后,它现在可以工作:

It works now, after these changes:

NSArray *namesOfContintents = [world allKeys];
NSString *ContinentName = [namesOfContintents objectAtIndex:indexPath.row];

cell.textLabel.text = ContinentName;

我还想在单元格中添加一些字幕.我加了:

I also wanted to add some subtitles to the cells. I added:

int numberEurope;
numberEurope = [europe count];

int numberAfrica;
numberAfrica = [africa count];

NSNumber *myNum1 = [NSNumber numberWithInt:numberEurope];
NSNumber *myNum2 = [NSNumber numberWithInt:numberAfrica];

NSArray *myArray = [NSArray arrayWithObjects: myNum1, myNum2, nil];

cell.textLabel.text = ContinentName;
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%@ countries", myArray];
return cell;

但是每个单元格中的字幕都相同:(2、2)个国家,而不是第一个单元格中的2个国家和第二个单元格中的2个国家.我在做什么错了?

But the subtitles are the same in every cell: ( 2, 2) countries instead of 2 countries in the first cell and 2 countries in the second one. What am I doing wrong?

推荐答案

NSString *ContinentName = [world objectForKey:indexPath.row];

objectForKey期望键的名称,您输入的是int.

the objectForKey is expecting a name for the key, you´re giving in an int.

尝试一下:

NSDictionary *ContinentName = [world objectForKey:@"Europe"];

此外,此处返回的值是NSDictionary,而不是字符串.

Also, the value returning here is a NSDictionary, not a String.

我建议将您的大洲转换为平面阵列.

I recommend converting your continents to a flat array.

NSMutableArray *flatArray = [[NSArray alloc] init]; 
for(id item in world){
    [flatArray addObject:item];
}

您可以通过indexPath.row

如果您想要各大洲的名称,可以致电.

If you want the names of the continents you can call.

NSArray *namesOfContintents = [world allKeys];

这篇关于无法使用.plist填充UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆