在 UITableView 中按字母顺序显示 .plist 键值

发布于 2024-10-15 14:53:38 字数 2957 浏览 5 评论 0原文

我在 iOS .plist 中有一组字典,其结构类似于以下内容:

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

我试图将每个字典中的 name 加载到 NSTableViewCell 中,然后在 NSTableView 中按字母顺序显示它们,类似于 iOS 中的联系人应用程序。

下面是我的 ViewController .h 和 .m。排序正常,但我无法将结果加载到 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

编辑:与此相关的另一个问题此处

I have an array of dictionaries in an iOS .plist structured similar to the following:

<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 the <key>name</key> from each dictionary into an NSTableViewCell then display them all alphabetically in an NSTableView similar to the Contacts App in iOS.

Below are my ViewControllers .h and .m. The sort is working, but I am not able to load the results into the 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手心的温暖 2024-10-22 14:53:38

将 ivar 添加到头文件中视图控制器的 @interface:

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

不要忘记释放sortedCountries:

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

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

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

Add this code (to read and sort the plist by country name) to your view controller's initWith... method:

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

Then use the following snippet to extract the values:

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

Don't forget to release sortedCountries:

- (void)dealloc
{
    ...
    [sortedCountries release];
    [super dealloc];
}
画尸师 2024-10-22 14:53:38

为您的文件创建一个 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"];
}

并且不要忘记在 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 方法中返回 [iOSPlist count] ;

Create an NSArray for your file:


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

and don't forget to return [iOSPlist count] in the - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method;

沫雨熙 2024-10-22 14:53:38

下面是从 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"];

Here is an example pulling the version number out of the info.plist. Use something similar to pull out your name key ( objectForKey:@"name")

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Info.plist"];
plist = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain]; 
NSString* version = [plist objectForKey:@"CFBundleVersion"];
萌梦深 2024-10-22 14:53:38

这是一个关于处理 plist 中的数据的 StackOverflow 问题。答案非常详细。

将 Plist (NSString) 解析为 NSDictionary

Here's a StackOverflow question on working with data in plists. The answers get quite detailed.

Parse Plist (NSString) into NSDictionary

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文