Cannot Feed UITableView with .plist

房东的猫 提交于 2020-01-16 19:21:06

问题


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.

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?

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

And here I have a problem:

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

Thank you for any help.

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;

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?


回答1:


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

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

try this:

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

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

This array you can access via the indexPath.row

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

NSArray *namesOfContintents = [world allKeys];


来源:https://stackoverflow.com/questions/20371773/cannot-feed-uitableview-with-plist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!