我看到的示例和教程之后,如何从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
后坠毁的消息。 – rdelmar 2013-04-24 00:53:06