Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying .plist key values alphabetically in an UITableView
    primarykey
    data
    text
    <p>I have an array of dictionaries in an iOS .plist structured similar to the following:</p> <pre><code>&lt;plist version="1.0"&gt; &lt;array&gt; &lt;dict&gt; &lt;key&gt;name&lt;/key&gt; &lt;string&gt;Afghanistan&lt;/string&gt; &lt;key&gt;government&lt;/key&gt; &lt;string&gt;Islamic Republic&lt;/string&gt; &lt;key&gt;population&lt;/key&gt; &lt;integer&gt;29121286 &lt;/integer&gt; &lt;/dict&gt; &lt;dict&gt; &lt;key&gt;name&lt;/key&gt; &lt;string&gt;Albania&lt;/string&gt; &lt;key&gt;government&lt;/key&gt; &lt;string&gt;Emerging Democracy&lt;/string&gt; &lt;key&gt;population&lt;/key&gt; &lt;integer&gt;2986952&lt;/integer&gt; &lt;/dict&gt; </code></pre> <p> </p> <p>I am trying to load the <code>&lt;key&gt;name&lt;/key&gt;</code> from each dictionary into an NSTableViewCell then display them all alphabetically in an NSTableView similar to the Contacts App in iOS.</p> <p>Below are my ViewControllers .h and .m. The sort is working, but I am not able to load the results into the TableViewCells? </p> <p><strong>FirstViewController.h</strong> </p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface FirstViewController : UIViewController &lt;UITableViewDelegate,UITableViewDataSource&gt; { NSArray *sortedCountries; } @property (nonatomic, retain) NSArray *sortedCountries; @end </code></pre> <p><strong>FirstViewController.m</strong></p> <pre><code>#import "FirstViewController.h" @implementation FirstViewController @synthesize sortedCountries; -(void)viewDidLoad { NSString *path = [[NSBundle mainBundle] pathForResource:@"countries"ofType:@"plist"]; NSArray *countries = [NSArray arrayWithContentsOfFile:path]; NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease]; NSArray *sortedCountries = [[countries sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]] retain]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 2; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *country = [sortedCountries objectAtIndex:indexPath.row]; NSString *countryName = [country objectForKey:@"name"]; static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text = countryName; return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [sortedCountries release]; [super dealloc]; } @end </code></pre> <p><strong>EDIT:</strong> Another question related to this <a href="https://stackoverflow.com/questions/4879691/instance-variable-not-retaining-value-in-ios-application">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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