0

当root是字典时,我在教程之后看到了如何从plist加载tableview的示例和教程,但我必须使用作为数组的plist。plist设置:

root  Array
   Item 0  Dictionary
         name String
   Item 1  Dictionary
         name String

...

- (void)viewDidLoad
{
    [super viewDidLoad];  

    NSString *path = [[NSBundle mainBundle] pathForResource:@"food" ofType:@"plist"];

    NSArray *tableData = [[NSArray alloc]initWithContentsOfFile:path];
    NSArray *thumbnails = [[NSArray alloc] init];

      for (NSDictionary *dict in tableData){

         NSLog(@"%@",dict); // prints all key value pairs in dictionary

         thumbnails = [dict objectForKey:@"name"];

      }
      NSLog(@"outside %@", thumbnails);  // this prints last value added to thumbnails array 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    NSLog(@"%@", thumbnails);  // this loads last value added to array thumbnails

    cell.textLabel.text = [thumbnails objectAtIndex:indexPath.row];

    return cell;
}

我做错了......它没有加载到我的表格视图中并崩溃。我认为我的 for 循环是错误的,我认为我的 objectAtIndex 是错误的,因为它在该行崩溃了。我非常乐意分享更多信息。我有数据源和 tableview 的委托连接到文件的所有者。我已经测试过 tableview 可以直接将数组加载到其中。请帮忙,我很感激。

编辑:

我在 .m 文件的顶部放置了可变数组缩略图的声明,如下所示:

@interface SimpleTableViewController ()
@property (nonatomic, strong) NSMutableArray *thumbnails;
@end

不幸的是,这些更改仍然让我在 cellForRowAtIndexPath 中留下空的缩略图数组

将 @property 放入 .h 文件时相同

编辑:.m 文件(最新代码)

#import "SimpleTableViewController.h" 

@interface SimpleTableViewController ()

@end

@implementation SimpleTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];   

   NSString *path = [[NSBundle mainBundle] pathForResource:@"food" ofType:@"plist"];

   NSArray *tableData = [[NSArray alloc]initWithContentsOfFile:path];
   NSMutableArray *thumbnails = [NSMutableArray array];

   for (NSDictionary *dict in tableData){
       NSLog(@"%@",dict); // prints all key value pairs in dictionary
      [thumbnails addObject:dict[@"name"]];
   }
       NSLog(@"outside %@", thumbnails);   

// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

   return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:simpleTableIdentifier];
    }
    NSLog(@"other method%@", self.thumbnails);  // this loads last value added to array thumbnails
    cell.textLabel.text = self.thumbnails[indexPath.row];

    return cell;


}

@end

.h 文件

#import <UIKit/UIKit.h>

@interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

@property (strong, nonatomic) NSMutableArray *thumbnails;

@end
4

2 回答 2

1

尝试

- (void)viewDidLoad
{
    [super viewDidLoad];  

    NSString *path = [[NSBundle mainBundle] pathForResource:@"food" ofType:@"plist"];

    NSArray *tableData = [[NSArray alloc]initWithContentsOfFile:path];

    NSMutableArray *thumbnails = [NSMutableArray array];

      for (NSDictionary *dict in tableData){
         NSLog(@"%@",dict); // prints all key value pairs in dictionary
         [thumbnails addObject:dict[@"name"];
      }
      NSLog(@"outside %@", thumbnails);  // this prints last value added to thumbnails array 
}

编辑 :

@property (nonatomic, strong) NSMutableArray *thumbnails;

- (void)viewDidLoad
    {
        [super viewDidLoad];  

        NSString *path = [[NSBundle mainBundle] pathForResource:@"food" ofType:@"plist"];

        NSArray *tableData = [[NSArray alloc]initWithContentsOfFile:path];

        self.thumbnails = [NSMutableArray array];

          for (NSDictionary *dict in tableData){
             NSLog(@"%@",dict); // prints all key value pairs in dictionary
             [self.thumbnails addObject:dict[@"name"];
          }
          NSLog(@"outside %@", self.thumbnails);  // this prints last value added to thumbnails array 
    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Initialize Cell

   cell.textLabel.text = self.thumbnails[indexPath.row];

   return cell;

}
于 2013-04-24T05:09:53.423 回答
0

您的代码有几个问题。

A. 查看提供的 plist 数据,您有一个字典数组。

B. 这一行,将缩略图分配为本地 NSArray。

NSArray *thumbnails = [[NSArray alloc] init];

但是这一行,self.thumbnails 是什么属性呢?它不是上面的缩略图。看起来谎言键@“name”包含一个数组而不是最终对象。

self.thumbnails = [dict objectForKey:@"name"];
于 2013-04-24T01:11:42.430 回答