我创建了一个如下的 plist 文件,所有部分都遵循相同的结构:
<array>
<dict>
<key>image</key>
<string>gradient1.png</string>
<key>name</key>
<string>Section 1</string>
<key>list</key>
<array>
<dict>
<key>questionTitle</key>
<string>How are you doing?</string>
<key>questionInfo</key>
<string>questionInfo1</string>
</dict>
<dict>
<key>questionTitle</key>
<string>What are you doing?</string>
<key>questionInfo</key>
<string>questionInfo2</string>
</dict>
<dict>
<key>questionTitle</key>
<string>Where are you going</string>
<key>questionInfo</key>
<string>questionInfo3</string>
</dict>
</array>
</dict>
我在嵌套表中使用它。我使用“名称”作为部分标题,并希望创建一个包含不同问题及其相关信息的新数组。
- (void) setCategoryArray
{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"QuestionList" withExtension:@"plist"];
NSArray *mainArray = [[NSArray alloc] initWithContentsOfURL:url];
NSMutableArray *categoryArray = [[NSMutableArray alloc] initWithCapacity:[mainArray count]];
for (NSDictionary *dictionary in mainArray) {
Category *category = [[Category alloc] init];
category.name = [dictionary objectForKey:@"name"];
category.list = [dictionary objectForKey:@"list"];
questionList = category.list;
NSLog(@"%@", questionList);
category.image = [dictionary objectForKey:@"image"];
[categoryArray addObject:category];
}
NSMutableArray *questionArray = [[NSMutableArray alloc] initWithCapacity:nil];
for (NSDictionary *dictionary in questionList) {
Category *question = [[Category alloc] init];
question.questionTitle = [dictionary objectForKey:@"questionTitle"];
question.questionInfo = [dictionary objectForKey:@"questionInfo"];
[questionArray addObject:question];
}
self.categoryList = categoryArray;
self.questionList = questionArray;
}
认为这可能是我如何创建 questionArray 或我在表格标签中引用它的方式的问题(见下文),但我为每个单元格获得相同的条目(但它不是重复的第一个或最后一个条目)。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SavedCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Category *question = (Category *)[self.questionList objectAtIndex:indexPath.section];
UILabel *questionNameLabel = (UILabel *)[cell viewWithTag:101];
questionNameLabel.text = question.questionTitle;
return cell;
}
非常感谢你的帮助!