我在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>
我正在尝试将每个字典中的<key>name</key>
加载到NSTableViewCell中,然后在NSTableView中按字母顺序显示它们,类似于iOS中的Contacts App。
以下是我的ViewControllers .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
编辑:与此here相关的另一个问题。
答案 0 :(得分:5)
将ivar添加到头文件中视图控制器的@interface:
@interface MyViewController : UITableViewController
{
...
NSArray *sortedCountries;
}
将此代码(按国家/地区名称读取和排序)添加到视图控制器的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];
}
答案 1 :(得分:2)
为您的文件创建NSArray:
NSArray *iOSPlist = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"iOS" ofType:@"plist"]];
然后在这个方法中写if if(cell == nil){
}:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.text = [[iOSPlist objectAtIndex:indexPath.row] objectForKey:@"name"];
}
并且不要忘记在 - (NSInteger)tableView中返回[iOSPlist count] :( UITableView *)tableView numberOfRowsInSection:(NSInteger)section method;
答案 2 :(得分:1)
以下是从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"];
答案 3 :(得分:1)
这是关于在plists中处理数据的StackOverflow问题。答案非常详细。