0

我创建了一个如下的 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;
}

非常感谢你的帮助!

4

1 回答 1

2

好的,我想我已经找到了你的问题。

您在第一个 for 循环中设置questionList为。category.list该 for 循环在questionArray循环运行之前完成(因此第二个 for 循环只会运行一个questionList- 最后一个)。

我认为您所追求的解决方案是将第二个 for 循环放在第一个循环中:

    for (NSDictionary *dictionary in mainArray) {
        Category *category = [[Category alloc] init];
        category.name = [dictionary objectForKey:@"name"];
        category.list = [dictionary objectForKey:@"list"];
        category.image = [dictionary objectForKey:@"image"];
        [categoryArray addObject:category];

        NSMutableArray *questionArray = [[NSMutableArray alloc] initWithCapacity:nil];
        for (NSDictionary *dictionary in category.list) {
            Category *question = [[Category alloc] init];
            question.questionTitle = [dictionary objectForKey:@"questionTitle"];
            question.questionInfo = [dictionary objectForKey:@"questionInfo"];
            [questionArray addObject:question];
        }
        [self.questionList addObject:questionArray];
    }
    self.categoryList = categoryArray;

然后问题cellForRowAtIndexPath将是:

Category *question = (Category *)[[self.questionList objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

我还没有对此进行测试,但希望它会起作用——或者使用一些 NSLog,让我们更接近一个可行的解决方案。

于 2013-02-06T12:33:51.660 回答