开发者

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 befo开发者_JAVA技巧re, 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. I.e., 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜