Display .plist key values ​​in alphabetical order in a UITableView

I have a set of dictionaries in iOS.plist, structured something like this:

<plist version="1.0">
<array>
<dict>
    <key>name</key>
    <string>Afghanistan</string>
    <key>government</key>
    <string>Islamic Republic</string>
    <key>population</key>
    <integer>29121286
    </integer>
</dict>
<dict>
    <key>name</key>
    <string>Albania</string>
    <key>government</key>
    <string>Emerging Democracy</string>
    <key>population</key>
    <integer>2986952</integer>
</dict>

I am trying to load <key>name</key>from each dictionary into NSTableViewCell, and then display them all alphabetically in NSTableView, similar to the Contacts application in iOS.

Below are my ViewControllers.h and .m. The sort works, but I can’t load the results in TableViewCells?

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController  <UITableViewDelegate,UITableViewDataSource>

{   
NSArray *sortedCountries;       
}

@property (nonatomic, retain) NSArray *sortedCountries;

@end

FirstViewController.m

#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

EDIT: Another question related to this here .

+3
source share
4 answers

Add ivar to your @interface view controller in the header file:

@interface MyViewController : UITableViewController
{
    ...
    NSArray *sortedCountries;
}

( plist ) initWith...:

NSArray *countries = [NSArray arrayWithContentsOfFile: pathToPlist];
// Now the array holds NSDictionaries, sort 'em:
NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease];
sortedCountries = [[countries sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]] retain];

:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *country = [sortedCountries objectAtIndex:indexPath.row];
    NSString *countryName = [country objectForKey:@"name"];
    NSString *governmentType = [country objectForKey:@"government"];
    NSSInteger population = [[country objectForKey:@"population"] integerValue];
    // ... do something with countryName, governmentType, population
}

:

- (void)dealloc
{
    ...
    [sortedCountries release];
    [super dealloc];
}
+5

NSArray :


NSArray *iOSPlist = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"iOS" ofType:@"plist"]];
then in this method write after if (cell == nil){

}:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.text = [[iOSPlist objectAtIndex:indexPath.row] objectForKey:@"name"];
}

[ iOSPlist] - (NSInteger) Tableview: (UITableView *) Tableview numberOfRowsInSection: (NSInteger) ;

+2

info.plist. - , (objectForKey: @ "name" )

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Info.plist"];
plist = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain]; 
NSString* version = [plist objectForKey:@"CFBundleVersion"];
+1
+1

Source: https://habr.com/ru/post/1789073/


All Articles