1

我在“支持文件”文件夹中有 PropertyList.plist 文件。我在里面做了一本字典。plist文件是:

在此处输入图像描述

我的 ViewController.m 文件代码是

   @implementation GroupedInexedViewController
{
    NSDictionary *names;
    NSArray *keys;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
                                                     ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc]
                          initWithContentsOfFile:path];
    names = dict;
    [dict release];
    NSArray *array = [[names allKeys] sortedArrayUsingSelector:
                      @selector(compare:)];
    keys = array;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [keys count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    return [nameSection count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    NSString *key  = [keys objectAtIndex:section];
    NSArray *nameSection = [names objectForKey:key];
    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier] autorelease];
    }
    cell.textLabel.text = [nameSection objectAtIndex:row];
    return cell;
}

-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *key = [keys objectAtIndex:section];
    return key;
}

不幸的是,“keys”数组不包含任何元素。因为我用它的计数值“keys.count”发出了警报,它是零。而且“名称”计数也为零。vieDidLoad 方法的路径变量显示了正确的路径。但它无法从 plist 文件的字典中读取。

编辑:我使用了 nslog,它显示在“viewDidLoad”方法中“names”能够加载字典。但是“keys”数组无法加载它。

4

2 回答 2

2

@EugeneK 说的有效,但在线上的“cellForRowAtIndexPath”方法中获得了 sigabrt

cell.textLabel.text = [nameSection objectAtIndex:row];

错误信息是

“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例 0x6832440”。

问题是 nameSection 变量显示为不支持 objectAtIndex 方法的 NSDictionary 类型对象。

所以我所做的我知道这不是一个很好的方法。我确信还有其他方法。我将“viewDidLoad”方法更改为此,

 - (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
                                                     ofType:@"plist"];
    names = [[NSDictionary alloc]
             initWithContentsOfFile:path];
    keys = [names allKeys];
    NSString *key = [keys objectAtIndex:0];
    names = [names objectForKey:key];
    keys = [[[names allKeys] sortedArrayUsingSelector:@selector(compare:)] retain];
    NSLog(@"keys = %@  names = %@",keys,names);
}

有用!不过,任何如何做得更好的想法都将不胜感激。

于 2012-11-14T06:52:44.247 回答
1

马丁 R是对的。请参阅代码中的注释:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
                                                     ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc]
                          initWithContentsOfFile:path]; //retainCount of dict is 1
    names = dict; // you made weak reference to dict
    [dict release]; // retainCount is 0 - dict is being dealloced
    NSArray *array = [[names allKeys] sortedArrayUsingSelector:
                      @selector(compare:)]; // you try to get data from dealloced object
    keys = array;
}

尝试执行以下操作:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"PropertyList"
                                                     ofType:@"plist"];
    names = [[NSDictionary alloc]
             initWithContentsOfFile:path];

    keys = [[[names allKeys] sortedArrayUsingSelector:
            @selector(compare:)] retain];
}

并且不要忘记释放nameskeys在您的视图控制器dealloc中。

于 2012-11-13T15:32:48.193 回答