Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot Feed UITableView with .plist
    text
    copied!<p>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.<br> <img src="https://i.stack.imgur.com/UE6fE.jpg" alt="plist"></p> <p>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?</p> <p>Here is my code: MasterViewController.h:</p> <pre><code>@interface MasterViewController : UITableViewController @property (nonatomic, strong) NSDictionary *world; @property (nonatomic, strong) NSDictionary *africa; @property (nonatomic, strong) NSDictionary *europe; @end </code></pre> <p>MasterViewController.m (the places where I added something):</p> <pre><code>@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 } </code></pre> <p>And here I have a problem:</p> <pre><code>- (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; } </code></pre> <p>How can I get my continents shown in the table view? How to get countries and then other info shown after another clicks?</p> <p>Thank you for any help.</p> <p>It works now, after these changes:</p> <pre><code>NSArray *namesOfContintents = [world allKeys]; NSString *ContinentName = [namesOfContintents objectAtIndex:indexPath.row]; cell.textLabel.text = ContinentName; </code></pre> <p>I also wanted to add some subtitles to the cells. I added:</p> <pre><code>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; </code></pre> <p>But the subtitles are the same in every cell: <strong>( 2, 2) countries</strong> instead of 2 countries in the first cell and 2 countries in the second one. What am I doing wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload