2013-04-24 53 views
0

我看到的示例和教程之后,如何从root用户加载tableview的视图是词典,但我必须使用plist是一个数组。 的plist设置:plist to tableview当根是数组然后字典

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

我这样做是错误的...它不会加载到我的tableview和崩溃。我认为我的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 
+0

后坠毁的消息。 – rdelmar 2013-04-24 00:53:06

回答

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; 

} 
+0

谢谢Anupdas,它为我的viewDidLoad方法中的缩略图创建了正确的值,但是在cellForRowAtIndexPath中它仍然是空的:-(。我试图在头文件中的任何地方声明这个NSMutableArray缩略图在实现b/c中知道它创建一个只是viewDidLoad方法,然后使用cellForRowAtIndexPath方法以外的一个不同的全球一个,但我不知道如何解决它。 – myData 2013-04-24 06:42:11

+0

@coreyData我编辑了我的答案。 – Anupdas 2013-04-24 07:05:51

+0

看到我的答案的结尾我已经为你编辑回复。再次感谢! – myData 2013-04-24 08:04:47

0

有几个不幸事故与您的代码。

A.查看提供的plist数据,您将获得一组字典。

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

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

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

self.thumbnails = [dict objectForKey:@"name"]; 
+0

我用缩略图替换了self.thumbnails,但现在在cellForRowAtIndexPath中,缩略图是一个空数组。它不再崩溃。在plist名字中也是字符串的关键。我需要在代码中的某处使用allKeys吗?感谢您的指导! – myData 2013-04-24 02:08:39