我看到示例和教程后教程如何在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;
}
我做错了......它没有加载到我的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 :(得分: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;
}
答案 1 :(得分:0)
你的代码有几处不幸。
一个。查看提供的plist数据,您有一系列字典。
B中。这一行,将缩略图分配为本地NSArray。
NSArray *thumbnails = [[NSArray alloc] init];
但是这一行,什么是self.thumbnails属性?它不是上面的缩略图。它看起来是谎言密钥@“name”包含一个数组而不是一个最终对象。
self.thumbnails = [dict objectForKey:@"name"];