简体   繁体   中英

Sectioned UITableView from pList that can drill down into children sub views

I feel really bad for asking this because someone helped me get half of it but im literally at witts end tryin to figure this out.

Basically i want a sectioned UITableView that loads its data from a pList file that can drill down into subviews.

As mentioned before, someone was nice enough to help me get the Sectioned UITableView from a pList but i cant figure out how to drill it down

Heres his Source that he provided to me

I'd show it here but i dont quite understand how to display code without it looking disgusting =/

you could just repeat the same trick for the deeper levels. Ie, have separate plist files called "Section1Row1.plist", "Section2Row2.plist", etc. Then you can reuse the same view controller class, but make the name of the plist file that it loads depend on the selection the user made.

The other approach is to store everything in one big plist file. Each option (row) then becomes a dictionary, containing the title and a whole new hierarchy like the original one. The "root" array of the new view controller is then set to the option you selected, like I show in the sample code and plist below.

Either approach will become messy though if you have many options or levels.

- (void) viewDidLoad
{
    [super viewDidLoad];
    if (!self.tableData)
    {
        self.tableData = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"Table" ofType: @"plist"]];
        self.title = @"Root";
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [[[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row] objectForKey: @"Title"];
    return cell;
}

- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath;
{
    RootViewController* nextViewController = [[[RootViewController alloc] initWithNibName: @"RootViewController" bundle: nil] autorelease];
    NSDictionary* rowData = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];
    NSArray* subtree = [rowData objectForKey: @"Subtree"];
    nextViewController.tableData = subtree;
    nextViewController.title = [rowData objectForKey: @"Title"];

    [self.navigationController pushViewController: nextViewController animated: YES];
}


<?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>Section1</string>
        <key>Rows</key>
        <array>
            <dict>
                <key>Title</key>
                <string>Row1</string>
                <key>Subtree</key>
                <array/>
            </dict>
            <dict>
                <key>Title</key>
                <string>Row2</string>
                <key>Subtree</key>
                <array>
                    <dict>
                        <key>Title</key>
                        <string>Table2Section1</string>
                        <key>Rows</key>
                        <array>
                            <dict>
                                <key>Title</key>
                                <string>Row1</string>
                                <key>Subtree</key>
                                <array/>
                            </dict>
                            <dict>
                                <key>Title</key>
                                <string>Row2</string>
                                <key>Subtree</key>
                                <array/>
                            </dict>
                        </array>
                    </dict>
                </array>
            </dict>
        </array>
    </dict>
</array>
</plist>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM