もっと深いレベルで同じトリックを繰り返すことができます。つまり、Section1Row1.plist、Section2Row2.plistなどのplistファイルを別に用意します。次に、同じView Controllerクラスを再利用できますが、ロードするplistファイルの名前は、ユーザーの選択に応じて変更してください。
もう1つの方法は、すべてを1つの大きなplistファイルに保存することです。各オプション(行)は辞書となり、タイトルと元の階層のような新しい階層が含まれます。新しいView Controllerの「ルート」配列は、次に示すサンプルコードとplistに示すように、選択したオプションに設定されます。
多くのオプションやレベルがある場合でもどちらの方法も面倒です。
- (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>
ヘイスティーブン、 もう一度お手伝いをしてください! :) とても有難い。すべてがほとんどdidSelectRowAtIndexPathを除いて、私は行の1つをクリックした後、それは無限ループ(私のために)それは行のサブツリーを表示することはありませんに行く。 =/ – Andyy
didSelectRowAtIndexPathの外観はどうですか? –
実際、私はそれが最後にサブツリーを追加するのを忘れてしまったので、ループしていたと思います。壮大な顔の手のひらの瞬間の笑、あなたの助けをもう一度非常に非常にありがとう。神のご加護を! – Andyy