我正在使用故事板为iPad做一个项目。我正在尝试用来自列表的数据填充我的 UITableView。Plist有一个NSDictionary,它又有9个数组,每个数组有三个元素(一个数字和两个字符串)。我想从每个数组(一个字符串)中获取第二个元素 [1],并用这些字符串填充表。
从头文件;
@interface EonOrderOfPrinciple : UIViewController
<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *formManeuvers;
@property (nonatomic, strong) NSDictionary *Wild;
@property (nonatomic, strong) NSArray *Smash;
@property (nonatomic, strong) NSArray *CloseCombat;
@property (nonatomic, strong) NSArray *HeadButt;
@property (nonatomic, strong) NSArray *Pummel;
@property (nonatomic, strong) NSArray *Obstacle;
@property (nonatomic, strong) NSArray *Confusion;
@property (nonatomic, strong) NSArray *ImpWeap;
@property (nonatomic, strong) NSArray *Rage;
@property (nonatomic, strong) NSArray *WildStorm;
从实施;
NSString *aFile = [[NSBundle mainBundle]
pathForResource:@"Wild" ofType:@"plist"];
Wild = [[NSDictionary alloc] initWithContentsOfFile:aFile];
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
NSString *currentManeuverName;
currentManeuverName = [Smash objectAtIndex:1];
[[cell textLabel] setText:currentManeuverName];
return cell;
}
@end
我意识到我现在只给它一个细胞。我想一旦我得到一个工作,我就可以尝试让其余的填充。现在我什至无法得到那个。
我没有找到任何教程和帮助来解释如何使用这样的嵌套数据。我尝试使用我发现的东西的各种版本,但没有任何效果。我想在进入核心数据之前学习如何使用plists。非常简单的plists我已经做得很好,似乎正是这个嵌套的业务让我绊倒了。
* 更新 10 月 29 日 ...我现在已经删除了 plist *
所以。如何使用嵌套字典列表正确访问信息并填充表。同样,这是使用嵌套字典。这就是我不知道该怎么做的,这就是我试图学习如何做的。
在问题中使用 PLIST 回答:
我认为你需要对如何构建你的PLIST有更多的反思。
在您的情况下,我认为最好做这样的事情:
Wild.plist
// Choose your key
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>title</key>
<string>Smash</string>
<key>letter</key>
<string>F</string>
<key>id</key>
<string>1</string>
</dict>
<dict>
<key>title</key>
<string>Close Combat</string>
<key>letter</key>
<string>W</string>
<key>id</key>
<string>2</string>
</dict>
</array>
</plist>
在您的 .h 中
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
在您的 .m 中:
// Your keys depend of your PLIST
#define KEY_TITLE @"title"
#define KEY_LETTER @"letter"
#define KEY_ID @"id"
// Data from your plist
@property (nonatomic, weak) NSMutableArray *data
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *a = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Wild" ofType:@"plist"]];
self.data = [NSMutableArray array];
for (NSDictionary *dic in a)
[data addObject:[dic objectForKey:KEY_TITLE]; // Define your key
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
cell.textLabel = [data objectAtIndex:indexPath.row];
return cell;
}
PLIST之前的旧答案:
您需要在一个新的可变数组中获取 PLIST 文件的所有第二个元素,并像@property
一样声明它:
@property (nonatomic, weak) NSMutableArray *data
在你的视图中DidLoad 构建这个数组:
(我认为您的 PLIST 带有第一个键的数组,请添加您的 PLIST 以获取更多信息)。
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *a = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Wild" ofType:@"plist"]];
self.data = [NSMutableArray array];
for (NSDictionary *dic in a)
[data addObject:[dic objectForKey:KEY_SECOND_ELEMENT]]; // KEY_SECOND_ELEMENT is the key on your plist
}
接下来的表视图:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
cell.textLabel = [data objectAtIndex:indexPath.row];
return cell;
}
您需要将 plist 加载到字典中并从中提取数组:
NSString *plistFile = [[NSBundle mainBundle] pathForResource:@"Wild" ofType:@"plist"];
NSDictionary *Wild = [[NSDictionary alloc] initWithContentsOfFile:plistFile];
self.Smash = [Wild objectForKey:@"Smash"];
self.CloseCombat = [Wild objectForKey:@"CloseCombat"];
self.HeadButt = [Wild objectForKey:@"HeadButt"];
.
.
.
然后,您需要将该数据与UITableViewCell相关联:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"cell"];
}
NSString *currentManeuverName;
currentManeuverName = [Smash objectAtIndex:1];
[[cell textLabel] setText:currentManeuverName];
return cell;
}
@end
您想以父键作为部分标题在部分和行中加载您的 plist!以下是您可以通过 3 个简单步骤做到这一点的方法。
步骤1: 首先,您不需要很多属性来保存父键。只需添加两个类级别变量,如下所示:
@interface EonOrderOfPrinciple () {
NSDictionary *parentDict;
NSArray *parentKeys;
}
步骤 2:在 viewDidLoad
方法中填充这些类级变量:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *aFile = [[NSBundle mainBundle] pathForResource:@"Wild" ofType:@"plist"];
parentDict = [NSDictionary dictionaryWithContentsOfFile:aFile];
parentKeys = [parentDict allKeys];
[self.formManeuvers reloadData];
}
步骤3:然后将它们加载到表数据源方法中:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return parentKeys.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return parentKeys[section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *parentKey = parentKeys[section];
NSDictionary *childDict = parentDict[parentKey];
return [childDict allKeys].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *parentKey = parentKeys[indexPath.section];
NSDictionary *childDict = parentDict[parentKey];
NSArray *childKeys = [childDict allKeys];
NSString *childKey = childKeys[indexPath.row];
cell.textLabel.text = childDict[childKey][1];
return cell;
}
我认为您在某处加载数据很好 Smash = (NSArray*)[Wild objectForKey:@"WhateverKeyIsForWild"];
在访问之前。
我不认为你的问题是嵌套词典或PLists或其他任何东西,因为你已经让它在这里和那里工作。(我是通过评论,而不是任何其他留言板)。
我相信让你得到的是iOS。您的加载函数是否在访问数组之前发生?尝试测试Smash是否加载了数据,在访问数据时记录其内容。
NSLog(@"%@", Smash); // should automatically dump its contents into the logger
如果什么都没有,零,垃圾,意外数据,那么你必须遵循你的代码并确保首先加载,或者内存不会被垃圾回收。
尝试禁用 ARC,您可以将代码复制并粘贴到没有它的新项目中。因为什么都不会改变(因为如果您只是测试几个字符串,泄漏是可以的)。如果它有效,你知道ARC就是它。
表的生命周期很有趣,但有些烦人,因为关于操作顺序,您只有几件事可以指望。
好吧,这个答案比我想要的要长得多,我为此道歉。责怪阿克。