简体   繁体   English

pList中的分段UITableView可以深入到子视图

[英]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. 基本上我想要一个分段的UITableView从pList文件加载其数据,该文件可以向下钻取到子视图。

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 如前所述,有人足够帮助我从pList中获取Sectioned UITableView,但我不知道如何进行深入研究

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. 即,具有单独的名为“ Section1Row1.plist”,“ Section2Row2.plist”等的plist文件。然后,您可以重用相同的视图控制器类,但根据用户所做的选择来确定要加载的plist文件的名称。

The other approach is to store everything in one big plist file. 另一种方法是将所有内容存储在一个大的plist文件中。 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. 然后,将新视图控制器的“根”数组设置为所选的选项,如我在下面的示例代码和plist中所示。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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