سؤال

I have a plist (here's a sample):

<?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>Setname</key>
        <string>Top 11</string>
        <key>Setnum</key>
        <integer>11</integer>
        <key>Verbsinset</key>
        <array>
            <dict>
                <key>Inf</key>
                <string>sein</string>
                <key>Present</key>
                <string>ist</string>
                <key>Prater</key>
                <string>war</string>
                <key>Pastpart</key>
                <string>gewesen</string>
                <key>Eng</key>
                <string>to be</string>
            </dict>
            <dict>
                <key>Inf</key>
                <string>haben</string>
                <key>Present</key>
                <string>hat</string>
                <key>Prater</key>
                <string>hatte</string>
                <key>Pastpart</key>
                <string>gehabt</string>
                <key>Eng</key>
                <string>to have</string>
            </dict>
            <dict>
                <key>Inf</key>
                <string>dürfen</string>
                <key>Present</key>
                <string>darf</string>
                <key>Prater</key>
                <string>durfte</string>
                <key>Pastpart</key>
                <string>gedurft</string>
                <key>Eng</key>
                <string>to be allowed (may)</string>
            </dict>
            <dict>
                <key>Inf</key>
                <string>müssen</string>
                <key>Present</key>
                <string>muss</string>
                <key>Prater</key>
                <string>musste</string>
                <key>Pastpart</key>
                <string>gemusst</string>
                <key>Eng</key>
                <string>to have to (must)</string>
            </dict>
        </array>
    </dict>
    <dict>
        <key>Setname</key>
        <string>Classroom 1</string>
        <key>Setnum</key>
        <integer>13</integer>
        <key>Verbsinset</key>
        <array>
            <dict>
                <key>Inf</key>
                <string>antworten</string>
                <key>Present</key>
                <string>antwortet</string>
                <key>Prater</key>
                <string>antwortete</string>
                <key>Pastpart</key>
                <string>geantwortet</string>
                <key>Eng</key>
                <string>to answer</string>
            </dict>

I can load a first array from the plist file and it is no problem to populate a table view with the list of Setnames/Setnumbers.

My problem is that when I drill down to the next table view, I want to list, for each different Set, the dictionary objects in that Set's nested Verbsinset array (e.g., the list of all the Inf keys belonging to that Set).

So I try to drill down through the plist, like so:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"ListSetCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    //Fetch item from plist
    NSDictionary *setoverbs = [self.verbsets objectAtIndex:[indexPath row]];
    NSArray *verblist = [setoverbs objectForKey:@"Verbinset"];
    NSDictionary *verbforms = [verblist objectAtIndex:[indexPath row]];

    //Configure the cell
    [cell.textLabel setText:verbforms[@"Inf"]];
    [cell.detailTextLabel setText:verbforms[@"Present"]];

    return cell;

}

for every Setname I press, the next view is the first verb from Set 1, the second verb from Set 2, and the 3rd verb from Set 3! (I have the number of rows to return set to 3 right now because I don't know how to tell it to count the right thing.)

Or, I can get the first verb from each set if I change the *verbforms reference to

NSDictionary *verbforms = [verblist objectAtIndex:0];

I could understand that getting the same list each time is because I haven't passed through which row was pressed (next battle), but right now I'm mystified as to why I can't pull the list from the array just from ONE Verbset?

Any help most appreciated! THANKS!

هل كانت مفيدة؟

المحلول 2

A programmer friend of mine discovered that the way to call the dictionary items for only one verbset was to use index position 0 in the first query, so:

NSDictionary *setoverbs = [self.verbsets objectAtIndex:0];
NSArray *verblist = [setoverbs objectForKey:@"Verbinset"];
NSDictionary *verbforms = [verblist objectAtIndex:[indexPath row]];

Subsequently, though, he replaced the first line to correspond to the actual row (in the higher-level controller) that was selected, making it simple to count the array verblist to return the correct number of rows. (This is especially easy to implement when you use the prepareForSegue method.)

نصائح أخرى

In your second code snippet, you have:

//Fetch item from plist
NSDictionary *setoverbs = [self.verbsets objectAtIndex:[indexPath row]];
NSArray *verblist = [setoverbs objectForKey:@"Verbinset"];
NSDictionary *verbforms = [verblist objectAtIndex:[indexPath row]];

You're using [indexPath row] as the index for both getting setoverbs from self.verbset and verbforms from verblist, which is probably not what you want—it does explain why you're getting the 1st one from Set 1, the 2nd from Set 2, and the 3rd from Set 3 (1-1, 2-2, 3-3).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top